Skip to content

Latest commit

 

History

History
262 lines (174 loc) · 8.62 KB

File metadata and controls

262 lines (174 loc) · 8.62 KB

Run containerized applications

The container is an instance of an image. Containers can be started, stopped, moved, and removed. In this section, we will look at the commands for working with containers.

Build an image

After the Dockerfile is created, you can start building the image. The image is usually built from the command line using the docker build command. The docker build command has the following syntax:

docker build [OPTIONS] PATH | URL | -

where PATH is the path to the directory containing the Dockerfile, URL is the URL of the Git repository containing the Dockerfile, - is standard input.

If the Dockerfile is in the current directory, then to build the image, it is enough to execute the command:

docker build .

In this case, the current directory will be used as the build context, and an image with an arbitrary name and tag latest will be created. If you need to specify the name and tag of the image, you can do this using the -t option:

docker build -t myimage:1.0 .

In this case, the image will be created with the name myimage and the tag 1.0.

To get a detailed description of the docker build command options, you can execute the command:

docker build --help

Viewing existing images

For viewing existing images, the docker images command is used:

docker images

The output of the command will contain the following columns:

  • REPOSITORIES - the name of the image, including the repository name
  • TAG - the image tag
  • IMAGE ID - the image identifier
  • CREATED - the image creation date
  • SIZE - the image size

Removing an image

An image can be removed using the docker rmi command:

docker rmi <image_name>

where <image_name> is the identifier or name of the image.

One command can remove several images at once, for this it is necessary to list their names separated by a space:

docker rmi <image_name_1> <image_name_2> <image_name_3>

Container creation

A container can be created based on an existing image using the docker create command:

docker create <image_name>

In this case, a container with an arbitrary name will be created based on the <image_name> image. If the image is not found locally, it will be downloaded from the repository. If you need to create a container with a specific name, then the --name flag is used:

docker create --name <container_name> <image_name>

A number of options can be specified when creating a container, such as mounting volumes, port forwarding, passing environment variables, etc. To get a detailed description of the docker create command options, you can execute the command:

docker create --help

Starting a container

After the container is created, it can be started using the docker start command:

docker start <container_name>

Developers often create and start a container in one command. For this, the docker run command is used:

docker run <image_name>

In this case, a container with an arbitrary name will be created and started based on the <image_name> image. If the image is not found locally, it will be downloaded from the repository.

Containers can be started in the background using the -d flag:

docker run -d <image_name>

If you need to create and start a container with a specific name, then the --name flag is used:

docker run -d --name <container_name> <image_name>

In this case, a container with a specific name <container_name> will be created and started based on the <image_name> image.

Interaction with the container

Interaction is possible only with a running container. To do this, you need to know its name or identifier. The container name can be set when creating the container using the --name flag, and the container identifier can be obtained using the docker ps command.

A command can be executed inside the container using the docker exec command:

docker exec <container_name> <command>

When the container is started, the command specified in the Dockerfile by one of the CMD or ENTRYPOINT directives is executed. If you need to execute another command, it is specified after the image name:

docker run <image_name> <command>

Sometimes it is necessary to run a container in interactive mode, for example, for debugging. Interaction with the container allows you to view its contents, execute commands in it (for example, install a package).

In order to run a container in interactive mode, the -it flag is used, which combines two flags -i (interactive) and -t (pseudo-TTY):

docker run -it <image_name> <command>

For example, to run a container with the ubuntu image in interactive mode, the command is used:

docker run -it ubuntu /bin/bash

Restarting a container

For restarting a container, the docker restart command is used. For example, to restart a container with the name my_container, the following command is used:

docker restart my_container

Copying files

For copying files, the docker cp command is used. The general syntax of the docker cp command is as follows:

docker cp <source> <destination>

where <source> is the source file or directory, and <destination> is the destination file or directory. The source or destination can be a path to a file or directory on the host or in the container. If the path is not specified, then the current directory is used.

For example, to copy the file.txt file from the container with the name my_container to the current directory, the following command is used:

docker cp my_container:/path/to/file.txt .

When it is necessary to copy file.txt from the host to the container with the name my_container from the current directory, the following command is used:

docker cp file.txt my_container:/path/to/file.txt

Viewing container logs

The logs of a container can be read using the docker logs command. For example, to read the logs of a container with the name my_container, the following command is used:

docker logs my_container

If you need to view logs in real time, then the -f flag is used:

docker logs -f my_container

Listing containers

A list of running containers can be viewed using the docker ps command:

docker ps

The output of the command will contain the following columns:

  • CONTAINER ID - container identifier
  • IMAGE - image name
  • COMMAND - command running in the container
  • CREATED - container creation date
  • STATUS - container status
  • PORTS - bound ports
  • NAMES - container name

When it is necessary to view all containers, including stopped ones, the -a flag is used:

docker ps -a

Stopping a container

A container can be stopped using the docker stop command:

docker stop <container_name>

When it is necessary to stop several containers at once, it is necessary to list their names separated by a space:

docker stop <container_name_1> <container_name_2> <container_name_3>

Removing a container

A container can be removed using the docker rm command:

docker rm <container_name>

When it is necessary to remove several containers at once, it is necessary to list their names separated by a space:

docker rm <container_name_1> <container_name_2> <container_name_3>

Bibliography

  1. How do I run a container?, docker.com
  2. Getting started guide, docker.com
  3. Запуск контейнера Docker, losst.pro, 2020