Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
150 views6 pages

Practical Tasks: Default

The document provides instructions for 10 tasks related to customizing Docker images. The tasks cover setting environment variables, copying files into an image, using build arguments, and ignoring files during builds. Completing the tasks helps learn how to define build steps and customize images using Dockerfiles.

Uploaded by

oyster66john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views6 pages

Practical Tasks: Default

The document provides instructions for 10 tasks related to customizing Docker images. The tasks cover setting environment variables, copying files into an image, using build arguments, and ignoring files during builds. Completing the tasks helps learn how to define build steps and customize images using Dockerfiles.

Uploaded by

oyster66john
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical tasks

Task #1. Docker installation

1. (Windows 10 Pro, Linux, OS X 10.11 and newer) Choose your operating system,
download and install Docker CE(community edition): https://docs.docker.com/install/
(Windows 10 Home, Windows 8 and earlier, OS X 10.10 and earlier) Download and
install Docker ToolBox: https://docs.docker.com/toolbox/toolbox_install_windows/
2. Check Docker version: docker --version
3. Run this command to view all the existing virtual machines: docker-machine ls.

Task #2. Working with virtual machines

1. View all the running virtual machines. What is the IP and URL of the virtual machine?
2. Run docker info command to view system information about Docker installation.
3. Run docker-machine version default and docker-machine env default commands. What is
their output?
4. Try to start/stop virtual machine: docker-machine start default / docker-machine stop
default

Task #3. Repositories

1. Create new account at http://cloud.docker.com (if you don’t have existing one)
2. Sign-in and create new public repository.
3. Open http://hub.docker.com (using created account) and review your repository details
4. Try to search on the site for java or mysql repositories. Which tags do they support?
5. Try to search repositories/images using Docker CLI, for example, docker seach java or
docker search --filter “is-official=true” java to find official images.
6. (Optional) Use can use Docker Hub REST API to view repository details, for example:
https://hub.docker.com/v2/repositories/library/java/

Task #4. Images

1. Run hello-world image: docker run hello-world


2. Display list of all running and stopped containers: docker ps -a. You can add -s flag to
view size occupied by container.
3. Try to run hello-world image using docker run --rm hello-world. What is the difference
with previous command?
4. Remove container: docker rm <ID>, where <ID> is first 3-5 numbers of container ID
5. Remove hello-world image: docker rmi hello-world
6. Download hello-world image again: docker pull hello-world and verify its existence in the
list using command: docker images. What is the image size?
7. Try to download tomcat of two different versions: docker pull tomcat:8 and docker pull
tomcat:9. Run docker images command. What is the difference between those images?
8. Run docker inspect tomcat:9 command. What are environment variables assigned? How
many layers are in this image?
9. Saves image: docker save hello-world > world.tar. Review achieve content.

Task #5. Containers

1. Run Tomcat 9 container: docker run tomcat:9 and then try to open http://localhost:8080
URL on the host machine. What is the result?
2. Run command docker ps to view all the running containers. What is the name of your
container?
3. Stop container: docker stop <container_ID> and then run container in the daemon
mode: docker run -d tomcat:9. What is the difference between daemon and non-
daemon mode? Try to view container logs using command docker logs <container_ID>
4. Now stop container and run command: docker run -p 8080:8080 tomcat:9. Try to open
http://localhost:8080/ Can you stop Tomcat using Ctrl-C keys?
(Windows 8 and earlier, Windows 10 Home). Try to run command: docker-machine ip
default and then use printed IP instead of localhost.
5. Stop container and run command: docker run -it -p 8080:8080 tomcat:9. This will start
container in the interactive mode. Try to press Ctrl-C again.
6. Stop container and run it using command: docker start <ID> or docker start -a <ID>
(interactive mode).
7. Run this command to rename container: docker rename <CONTAINER_ID>
<NEW_CONTAINER_NAME>. Can you rename container if it’s running?
8. Try to run Tomcat container: docker run -d -P tomcat:9. What are -d and -P flags? Can
you now access Tomcat in your host machine?
9. Exports container: docker export <ID> > world.tar, where <ID> is identifier of the
Tomcat9 container. Review world.tar content
10. If you want to provide a meaningful name for container, use the command docker run
--name tomcat9 tomcat:9. Try to run docker ps command and verify that container has
new name.
11. Run command docker exec -it <ID> /bin/sh, where <ID> is container identifier to run
bash shell on the running container. Run ls command to view file system on the running
container.
12. Run new Ubuntu container: docker run -it ubuntu bash
13. Run command in the new Docker console: docker exec <ID> /bin/sh -c “mkdir /test”
14. Verify in the first console that /test folder has appeared.
15. Run new Tomcat container and then execute command to pause this container: docker
pause <CONTAINER_ID>. Run docker ps command. What is the container status? You can
use docker unpause <CONTAINER_ID> command to make container alive.

Task #6. Creating images

1. Run new Ubuntu container(or use existing one), go to the container bash and execute
commands to install mc(Midnight commander):
apt-get update
apt-get install -y mc
2. Run mc command to verify that mc has been installed
3. Run command docker commit <ID> mc-local to create new image with name mc-local.
Run command docker images to verify that image has been created. What is the size of
new image?
4. Start this image inside container and execute bash console to verify that mc still exists
on file system.
5. Run Ubuntu container using command: docker run -it ubuntu echo ‘Hello!’ How does it
work? Does it start container?
6. Run new Ubuntu container: docker run -it ubuntu /bin/sh. Create folder test in the bash
console.
7. Create text file desc.txt in the current folder on the host file system.
8. Run command docker cp desc.txt <ID>:/test to copy desc.txt to the test folder of the
started container. Check in the bash console that file has been copied.
9. Run docker stats command to view detailed statistics about running containers and
docker stats -a to view statistics about all (even stopped) containers.
10. Run docker system df command to view disk usage by Docker
11. Run docker system info command to view Docker system information.
12. Run docker system prune command to remove all stopped containers.

Task #7. Resource constraints

1. Run Tomcat 9 container: docker run -d --name=tomcat9 tomcat:9


2. Open bash console in the container: docker exec -it tomcat9 /bin/sh
3. Install ps utility: apt-get update && apt-get install -y procps
4. Run ps aux command in the console and review resource utilization(CPU, Memory)
5. Stop container and run it again using command: docker run -d --name=tomcat9
--memory=64M --cpus=1 tomcat:9
6. Review resource utilization using docker stats command
7. (Optional) Change device I/O reading speed

Task #8. Building images


1. Run docker info command. What is your storage driver and backing file system?
2. Create new empty folder images.
3. Create empty file Dockerfile there.
4. Create docs folder in the images folder and put there file instructions.txt with
description of your image.
5. Add the following lines to the Dockerfile:
FROM ubuntu
LABEL Author=“Author” Company=”My company”
6. Run docker build . command in the images folder. What is its output? Run docker
images command and verify new image existence.
7. Provide username/repository from the Docker Hub and run command: docker build -t
<username>/<repository> . Run again docker images command. What has changed?
8. If you want to tag your image you can use command: docker tag <image>
<image>:<tag> or in the build directly: docker build -t <image>:<tag> .
9. Try to produce image with multiple tags: docker build -t <image> -t <image>:tag .
10. Run docker inspect <username>/<repository> command and try to find values from
LABEL instruction.
11. Authenticate yourself on Docer hub using docker login command.
12. Pushes your image using docker push <username>/<repository> command. Verify at
http://hub.docker.com that your repository was updated.
13. Run docker history <IMAGE_ID> command or docker history --no-trunc <IMAGE_ID>
command. What is its output?

Task #9. Files/executables

1. Add new line to Dockerfile:


RUN echo ‘Building image …’
2. Build image. Does ‘Building image …’ message appear during build? Does it appear if you
start build again? Run docker images command. Did new images appear in the list?
3. If you want to avoid caching add --no-cache=true flag when building image: docker build
--no-cache=true .
4. Add new lines to Dockerfile:
COPY docs/ /docs/
WORKDIR /docs
RUN cat instructions.txt
5. Build image again and verify that content of instructions.txt is displayed during build.
6. Add new line to Dockerfile:
ENTRYPOINT [“/bin/sh”]
7. Build image and run it: docker run -it <username>/<repository>. Verify that bash
prompt appears after start.
8. Copy file from container to your host file system (container must be running): docker cp
<CONTAINER_ID>:/docs/instructions.txt .
9. Add .dockerignore file to the root folder of build context. Add a line to it(*). Repeat build
of your image. What has changed?

Task #10. Customization

1. Add new line after ‘FROM ubuntu’:


ENV message=’Building message …’
2. Use new environment variable in the script: RUN echo $message
3. Build image again and confirm that message is displayed.
4. Replace ENV line with two lines:
ARG MESSAGE_ARG=’Building message’
ENV message $MESSAGE_ARG
5. Build image and confirm that message is displayed.
6. Run build using command: docker build --build-arg MESSAGE_ARG=Test -t
<username>/<repository> . Verify that you override argument value.
7. Run container in interactive mode and execute echo $message command. Verify that
“Test” is displayed.
8. Run container using command docker run -it -e message=”Prod”
<username>/<repository> Execute echo $message command and verify that “Prod” is
displayed.
9. Add new instruction ARG UBUNTU_VERSION=latest as first line in the script. Change
second line to FROM ubuntu: ${UBUNTU_VERSION}. Then try to use different base
images of Ubuntu, for example: docker build -t <username>/<repository> --build-arg
UBUNTU_VERSION=16.04 .
10. Rename Dockerfile to app.dockerfile and try to build it using command: docker build -f
app.dockerfile .
11. (Optional) Write Dockerfile that install and run mc (Midnight commander) during
container start.

Task #11. Dangling images

1. Run command docker images -a. What are the images with no name and tag?
2. Create new Dockerfile that contains two lines:
FROM ubuntu
RUN echo hello
3. Build this image
4. Change second line in Dockerfile to RUN echo hello2
5. Build this image again. Now run command docker images. Are there new images? Run
command docker images -f "dangling=true". Are there any images displayed?
6. Run this command to remove all the dangling images: docker image prune
Task #12. Docker CLI new syntax

1. Run command docker image ls to view all images.


2. Run command docker container ls to view all the containers.
3. Use command docker container exec to execute command inside the container.
4. Use command docker image build to build an image
5. Use command docker container create to run container
6. Use command docker container prune to remove all stopped containers
7. Run command docker image prune to remove all the dangling images.
8. Run command docker images prune -a to remove all the images not referenced by any
container.

Task #13. Volumes

1. Create new volume using command: docker volume create shared


2. Verify that volume has created: docker volume ls. Review volume information: docker
volume inspect shared. What is mountpoint of the volume?
3. Run a container using command: docker run -it -v shared:/shared <image>.
4. Verify that volume has been mounted: docker inspect -f “{{ .Mounts }}” <container_id>
5. Run a second container: docker run -it -v shared:/shared <image>
6. Open bash console in the first container: docker exec -it <container_id> /bin/sh and
create new file in the /shared folder: touch data.txt
7. Open bash console in the second container and confirm that /shared folder also
contains data.txt file.
8. Create new folder ROOT on the host machine and put sample index.html file there.
For example, it is folder /home/user1/ROOT.
(Windows) It is recommended to create ROOT folder in the home folder of the current
user.
9. Run Tomcat 9 container using command: docker run -v
/home/user1/ROOT:/usr/local/tomcat/webapps/ROOT -p 8080:8080 tomcat:9. Approves
file sharing prompts if it’s displayed. Open URL http://localhost:8080/ and verify that
content of your index.html file is displayed. If you have some issues on Windows system
you can find solution here: https://github.com/docker/for-win/issues/738
10. (Windows) If your share folder c:/Images/ROOT then you should pass it
as //c/Images/ROOT in the docker run command.
11. If you want to specify relative path to your local folder on Linux/MacOS system you can
use `pwd` command: docker run -v `pwd`/ROOT:/usr/local/tomcat/webapps/ROOT -p
8080:8080 tomcat:9.
12. Stop container and try to use new mount format: docker run --mount type=bind,
source=/home/user1/ROOT, target=/usr/local/tomcat/webapps/ROOT -p 8080:8080
tomcat:9.

You might also like