academy pro: docker. part 2
TRANSCRIPT
Containerization BasicsAcademyPRO
Today’sagenda
● Vocabulary
● Run static website
● Build & Run own image
● Logs
Vocabulary: Images
Images - The file system and configuration of our application which are used to
create containers.
Vocabulary: Containers
Containers - Running instances of Docker images — containers run the actual
applications.
A container includes an application and all of its dependencies. It shares the
kernel with other containers, and runs as an isolated process in user space on
the host OS.
A list of running containers can be seen using the docker ps command.
Vocabulary: Daemon & Client
Docker daemon - The background service running on the host that manages
building, running and distributing Docker containers.
Docker client - The command line tool that allows the user to interact with the
Docker daemon.
Vocabulary: Hub
Docker Hub - A registry of Docker images. You can think of the registry as a
directory of all available Docker images.
Run a static website in a container
docker run nginx
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
585e0876dad1 nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 80/tcp, 443/tcp goofy_payne
docker stop 585e0876dad1
docker rm 585e0876dad1
Run a static website in a container (Successful)
docker run -d -P nginx
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
585e0876dad1 nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 0.0.0.0:32769->80/tcp, goofy_payne
0.0.0.0:32768->443/tcp
docker port goofy_payne
443/tcp -> 0.0.0.0:32768
80/tcp -> 0.0.0.0:32769
Run a static website in a container (Successful) Attempt #2
docker run --name static-site -d -p 8888:80 nginx
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0f21f39e4cfa nginx "nginx -g 'daemon ..." About a minute ago Up About a minute 0.0.0.0:8888->80/tcp, static-site
443/tcp
docker rm -f static-site
Docker Images
To see the list of images that are available locally on your system, run the
docker images command.
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
busybox latest 00f017a8c2a6 2 months ago 1.11 MB
hello-world latest 48b5124b2768 3 months ago 1.84 kB
nginx latest 4a88d06e26f4 7 months ago 184 MB
Also you could pull a specific version of image as follows:docker pull node:0.10
Docker search
To get a new Docker image you can either get it from a registry (such as the
Docker Hub) or create your own. There are hundreds of thousands of images
available on Docker hub. You can also search for images directly from the
command line using docker search.
Base & Child Images
An important distinction with regard to images is between base images and
child images.
● Base images are images that have no parent images, usually images with
an OS like ubuntu, alpine or debian.
● Child images are images that build on base images and add additional
functionality.
Official & User ImagesOfficial
Another key concept is the idea of official images and user images. (Both of
which can be base images or child images.)
● Official images are Docker sanctioned images. Docker, Inc. sponsors a
dedicated team that is responsible for reviewing and publishing all Official
Repositories content. This team works in collaboration with upstream
software maintainers, security experts, and the broader Docker
community. These are not prefixed by an organization or username. In the
list of images above, the python, node, alpine and nginx images are official
(base) images.
Official & User ImagesUser
● User images are images created and shared by users like you. They build
on base images and add additional functionality. Typically these are
formatted as user/image-name. The user value in the image name is your
Docker Hub user or organization name.
Dockerfile commands summary FROM
FROM starts the Dockerfile. It is a requirement that the Dockerfile must start
with the FROM command. Images are created in layers, which means you can
use another image as the base image for your own. The FROM command
defines your base layer. As arguments, it takes the name of the image.
Optionally, you can add the Docker Hub username of the maintainer and image
version, in the format username/imagename:version.
Dockerfile commands summary RUN
RUN is used to build up the Image you're creating. For each RUN command,
Docker will run the command then create a new layer of the image. This way
you can roll back your image to previous states easily. The syntax for a RUN
instruction is to place the full text of the shell command after the RUN (e.g., RUN
mkdir /user/local/foo). This will automatically run in a /bin/sh shell.
You can define a different shell like this: RUN /bin/bash -c 'mkdir
/user/local/foo'
Dockerfile commands summary COPY & CMD
COPY copies local files into the container.
CMD defines the commands that will run on the Image at start-up. Unlike a RUN,
this does not create a new layer for the Image, but simply runs the command.
There can only be one CMD per a Dockerfile/Image. If you need to run multiple
commands, the best way to do that is to have the CMD run a script. CMD
requires that you tell it where to run the command, unlike RUN. So example CMD
commands would be:
CMD ["python", "./app.py"]
CMD ["/bin/bash", "echo", "Hello World"]
Dockerfile commands summary Expose & Push
EXPOSE opens ports in your image to allow communication to the outside
world when it runs in a container.
PUSH pushes your image to Docker Hub, or alternately to a private registry
Build image
docker build -t oleksandrkovalov/node_example .
docker run -d -p 8080:3000 oleksandrkovalov/node_example
Push image
docker login
docker push oleksandrkovalov/node_example
The push refers to a repository [docker.io/oleksandrkovalov/node_example]
a6cc0f5939b9: Pushed
...
5d6cbe0dbcf9: Pushed
latest: digest: sha256:239f102592ae13843da87bd61225fe40c686e869ff747f6e613dde8806c99009 size: 2837
Container logging
● Container PID 1 process output can be viewed with docker logs command
● Will show whatever PID 1 writes to stdout
View the output of the containers PID 1 process
docker logs <container name>
View and follow the output
docker logs -f <container name>
Container application logs
● Typically, apps have a well defined log location
● Map a host folder to the application’s log folder in the container
● In this way you can view the log generated in the container from your host
folder
Run a container using nginx image and mount a volume to map the
/tmp/nginxlogs folder in the host to the /var/log/nginx folder in the
container
docker run -d -P -v /tmp/nginxlogs:/var/log/nginx nginx
Private registry
● Allows you to run own registry instead of using Docker Hub
● Multiple options○ Run registry server using container
○ Docker Hub Enterprise
● Two versions○ Registry 1.0 for Docker 1.5 and below
○ Registry 2.0 for Docker 1.6
Setting up private registry
● Run a registry inside a container
● Use the registry image at http://registry.hub.docker.com/u/library/registry
● Image contains pre-configured registry v2.0
Run registry:
docker run -d -p 5000:5000 registry
Push and pull from private registry
● First tag image with host IP or domain of registry server, then run docker
push
Tag image and specify the registry host:
docker tag <image id> localhost:5000/my-app:1.0
Push image to registry:
docker push localhost:5000/my-app:1.0
Pull image from registry:
docker pull localhost:5000/my-app:1.0
Any questions?
The endfor today :)