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 &LTimage-name>:&LTimage-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 &LTimage-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 &LTimage-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 &LTcontainer-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 &LTcontainer-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 &LTcontainer-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 &LTimage-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:

  1. Create your own Dockerfile or download an existing one
  2. Move the Dockerfile to the project directory in which you want to create the image
  3. Run the command line tool and change the directory to the project directory
  4. 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.

Request more information today
Discover how our services can benefit your business. Leave your contact information and our team will reach out to provide you with detailed information tailored to your specific needs. Take the next step towards achieving your business goals.
Contact form:
Our latest news