Working with Docker sometimes requires running containers on a local machine. Below, we will examine how to run an image on the local machine and how to use a Dockerfile.
Frequently Used Commands
First, look at the commands to create, manage, and run images and containers.
Images
Return all available images. The name, tag, and size will be shown for each one. Example:
docker images
With the -q
flag, the command will only show IDs without detailed information. This option is useful if the resulting list is passed to the next command using the pipeline. For example, a user wants to remove all images. It can get a list of their IDs (as shown in the example below) and pass it to the rmi command. Example:
docker images -q
Build
Build an image with specified parameters and flags. Example:
docker build -t <image-name>:<image-tag>
You can add a tag to the built image with the -t
flag.
Pull
Download an image from a remote source to your local machine. Example:
docker pull <image-name>
Run
Run a new container from the selected image. It also allows you to configure additional options and run configurations, like running mode or port mapping. Example:
docker run -d -p 8080:80 <image-name>
The -d flag allows the user to launch the container in the background (the so-called “detached mode”). If it is successfully launched, the command-line tool returns the ID and then the terminal prompt. The -p
flag allows you to configure the mapping of the host (local machine) and virtual machine ports.
PS
It allows you to view a list of containers, as well as detailed information about each of them (ID, name, status, ports). Example:
docker ps
By default, only running containers will be shown. To view the full list, the user needs to add the -a
flag. With the -q
flag, the command will only show container IDs without detailed information. This option is useful if the resulting list of containers is passed to the next command using the pipeline. For example, a user wants to start all containers. It can get a list of their IDs (as shown in the example below) and pass it to the docker start container command. Example:
docker ps -a -q
Start
It is used to start the existing container(s). Example:
docker start <container-names-or-ids>
It is possible to start them all at once by getting a list of them with the additional ps command using the pipeline. Example:
docker start $(docker ps -a -q)
Stop
It is used to stop running container(s). Example:
docker stop <container-name-or-id>
It is possible to stop all containers at once by getting a list of them with the additional ps
command using the pipeline. Example:
docker stop $(docker ps -a -q)
RM
It is used to remove selected container(s). Example:
docker rm <container-name>
It is possible to remove all containers at once by getting a list of them with the additional ps command using the pipeline. Example:
docker rm $(docker ps -a -q)
RMI
Remove selected image(s). Example:
docker rmi <image-name>
It is possible to remove all images at once by getting a list of them with the additional images command using the pipeline. Example:
docker rmi $(docker images -q)
Dockerfile Usage
The software development and deployment process often requires building new images repeatedly. This process may be automated using the so-called Dockerfile.
A Dockerfile is a regular text file that contains a set of commands (a script) for building an image. When the command to build it is executed, it will run the Docker file and execute the script written in it.
How to Run Dockerfile
To build an image using a Dockerfile, the following steps must be performed:
- Create your own Dockerfile or download an existing one
- Move the Dockerfile to the project directory in which you want to create the image
- Run the command line tool and change the directory to the project directory
- Run the
build
command with the required flags and parameters
In this case, the build
command automatically uses the Dockerfile.
How to Run a Docker Container on Your Local Machine
Using the commands described above, running containers on a local machine will be very easy. Execute the following steps:
- Download Dockerfile or create it yourself and move it to the project directory
- Using the command line, build an image
docker build -t test-image
- Run a new container using the created image
docker run -p 9000:80 -t test-image
- Check the list of containers to make sure that the required one has been created and launched
docker ps
- Configure the environment properties if necessary. For the virtual machine to work correctly with the container, you may need to configure its name, virtual machine and host addresses, and the user profile.
These steps will create an image named test-image
, which will be launched in a dedicated virtual machine running the Linux operating system. In this case, port 80 of the virtual machine will map to port 9000 of the local machine (host). In the future, the operation of the created container can be stopped and restored using the stop
and start
commands, respectively.
Conclusions
Sometimes, you may need to run Docker locally on your machine.
You must create an appropriate image using the build
command to do this. To automate and simplify this process, you can use the Dockerfile, which contains a set of commands for creating an image. You can create it yourself if there is no ready-made Dockerfile suitable for the project. It is important to move the Dockerfile to the same directory in which the build
command will be executed.
When the image is successfully created, you should run it in a dedicated container using the run
command. The container will automatically create a virtual machine running the Linux operating system. When starting the container, you must specify the mapping for the host and virtual machine ports. You should also ensure that the environment properties are configured correctly.
Netflix tech stack for powering streaming backend and cloud solutions
Netflix cloud architecture for scalable streaming and seamless content delivery
API Integration Basis Patterns for seamless system communication and optimized software development