How to run docker image

How to run docker image

Run your image as a container

Estimated reading time: 10 minutes

Prerequisites

Work through the steps to build a Node JS image in Build your Node image.

Overview

A container is a normal operating system process except that this process is isolated and has its own file system, its own networking, and its own isolated process tree separate from the host.

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter and that is the image name. Let’s start our image and make sure it is running correctly. Execute the following command in your terminal.

When you run this command, you’ll notice that you were not returned to the command prompt. This is because our application is a REST server and will run in a loop waiting for incoming requests without returning control back to the OS until we stop the container.

Let’s open a new terminal then make a GET request to the server using the curl command.

Our curl command failed because the connection to our server was refused. It means that we were not able to connect to localhost on port 8000. This is expected because our container is running in isolation which includes networking. Let’s stop the container and restart with port 8000 published on our local network.

To stop the container, press ctrl-c. This will return you to the terminal prompt.

Start the container and expose port 8000 to port 8000 on the host.

Now let’s rerun the curl command from above. Remember to open a new terminal.

Success! We were able to connect to the application running inside of our container on port 8000. Switch back to the terminal where your container is running and you should see the POST request logged to the console.

2020-09-01T17:36:09:8770 INFO: POST /test

Press ctrl-c to stop the container.

Run in detached mode

Docker started our container in the background and printed the Container ID on the terminal.

Again, let’s make sure that our container is running properly. Run the same curl command from above.

List containers

Since we ran our container in the background, how do we know if our container is running or what other containers are running on our machine? Well, we can run the docker ps command. Just like on Linux, to see a list of processes on your machine we would run the ps command. In the same spirit, we can run the docker ps command which will show us a list of containers running on our machine.

The ps command tells a bunch of stuff about our running containers. We can see the Container ID, the image running inside the container, the command that was used to start the container, when it was created, the status, ports that exposed and the name of the container.

You are probably wondering where the name of our container is coming from. Since we didn’t provide a name for the container when we started it, Docker generated a random name. We’ll fix this in a minute but first we need to stop the container. To stop the container, run the docker stop command which does just that, stops the container. You will need to pass the name of the container or you can use the container id.

Now rerun the docker ps command to see a list of running containers.

Stop, start, and name containers

If you’ve been following along, you should see several containers listed. These are containers that we started and stopped but have not been removed.

Let’s restart the container that we just stopped. Locate the name of the container we just stopped and replace the name of the container below in the restart command.

Now, list all the containers again using the ps command.

Notice that the container we just restarted has been started in detached mode and has port 8000 exposed. Also, observe the status of the container is “Up X seconds”. When you restart a container, it will be started with the same flags or commands that it was originally started with.

Let’s stop and remove all of our containers and take a look at fixing the random naming issue.

Stop the container we just started. Find the name of your running container and replace the name in the command below with the name of the container on your system.

Now that all of our containers are stopped, let’s remove them. When a container is removed, it is no longer running nor is it in the stopped status. However, the process inside the container has been stopped and the metadata for the container has been removed.

To remove a container, simply run the docker rm command passing the container name. You can pass multiple container names to the command in one command.

Again, make sure you replace the containers names in the below command with the container names from your system.

Now let’s address the pesky random name issue. Standard practice is to name your containers for the simple reason that it is easier to identify what is running in the container and what application or service it is associated with. Just like good naming conventions for variables in your code make it simpler to read, so does naming your containers.

Now, we can easily identify our container based on the name.

Next steps

In this module, we took a look at running containers, publishing ports, and running containers in detached mode. We also took a look at managing containers by starting, stopping, and restarting them. We also looked at naming our containers so they are more easily identifiable. In the next module, we’ll learn how to run a database in a container and connect it to our application. See:

Feedback

Help us improve this topic by providing your feedback. Let us know what you think by creating an issue in the Docker Docs GitHub repository. Alternatively, create a PR to suggest updates.

Run your image as a container

Estimated reading time: 10 minutes

Prerequisites

Work through the steps to build a Python image in Build your Python image.

Overview

In the previous module, we created our sample application and then we created a Dockerfile that we used to produce an image. We created our image using the docker command docker build. Now that we have an image, we can run that image and see if our application is running correctly.

A container is a normal operating system process except that this process is isolated in that it has its own file system, its own networking, and its own isolated process tree separate from the host.

To run an image inside of a container, we use the docker run command. The docker run command requires one parameter which is the name of the image. Let’s start our image and make sure it is running correctly. Run the following command in your terminal.

After running this command, you’ll notice that you were not returned to the command prompt. This is because our application is a REST server and runs in a loop waiting for incoming requests without returning control back to the OS until we stop the container.

Let’s open a new terminal then make a GET request to the server using the curl command.

As you can see, our curl command failed because the connection to our server was refused. This means, we were not able to connect to the localhost on port 5000. This is expected because our container is running in isolation which includes networking. Let’s stop the container and restart with port 5000 published on our local network.

To stop the container, press ctrl-c. This will return you to the terminal prompt.

We did not specify a port when running the flask application in the container and the default is 5000. If we want our previous request going to port 5000 to work we can map the host’s port 8000 to the container’s port 5000:

Now, let’s rerun the curl command from above. Remember to open a new terminal.

Success! We were able to connect to the application running inside of our container on port 8000. Switch back to the terminal where your container is running and you should see the GET request logged to the console.

Press ctrl-c to stop the container.

Run in detached mode

Docker started our container in the background and printed the Container ID on the terminal.

Again, let’s make sure that our container is running properly. Run the same curl command from above.

List containers

Since we ran our container in the background, how do we know if our container is running or what other containers are running on our machine? Well, we can run the docker ps command. Just like on Linux to see a list of processes on your machine, we would run the ps command. In the same spirit, we can run the docker ps command which displays a list of containers running on our machine.

The docker ps command provides a bunch of information about our running containers. We can see the container ID, The image running inside the container, the command that was used to start the container, when it was created, the status, ports that exposed and the name of the container.

You are probably wondering where the name of our container is coming from. Since we didn’t provide a name for the container when we started it, Docker generated a random name. We’ll fix this in a minute, but first we need to stop the container. To stop the container, run the docker stop command which does just that, stops the container. You need to pass the name of the container or you can use the container ID.

Now, rerun the docker ps command to see a list of running containers.

Stop, start, and name containers

You should now see several containers listed. These are containers that we started and stopped but have not been removed.

Let’s restart the container that we just stopped. Locate the name of the container we just stopped and replace the name of the container below in the restart command.

Now list all the containers again using the docker ps command.

Notice that the container we just restarted has been started in detached mode and has port 8000 exposed. Also, observe the status of the container is “Up X seconds”. When you restart a container, it starts with the same flags or commands that it was originally started with.

Now, let’s stop and remove all of our containers and take a look at fixing the random naming issue. Stop the container we just started. Find the name of your running container and replace the name in the command below with the name of the container on your system.

Now that all of our containers are stopped, let’s remove them. When you remove a container, it is no longer running, nor it is in the stopped status, but the process inside the container has been stopped and the metadata for the container has been removed.

To remove a container, simple run the docker rm command passing the container name. You can pass multiple container names to the command using a single command. Again, replace the container names in the following command with the container names from your system.

Now, let’s address the random naming issue. Standard practice is to name your containers for the simple reason that it is easier to identify what is running in the container and what application or service it is associated with.

That’s better! We can now easily identify our container based on the name.

Next steps

In this module, we took a look at running containers, publishing ports, and running containers in detached mode. We also took a look at managing containers by starting, stopping, and, restarting them. We also looked at naming our containers so they are more easily identifiable. In the next module, we’ll learn how to run a database in a container and connect it to our application. See:

Feedback

Help us improve this topic by providing your feedback. Let us know what you think by creating an issue in the Docker Docs GitHub repository. Alternatively, create a PR to suggest updates.

Run your image as a container

Estimated reading time: 9 minutes

Prerequisites

Work through the steps to dockerize a Go application in Build your Go image.

Overview

A container is a normal operating system process except that this process is isolated and has its own file system, its own networking, and its own isolated process tree separate from the host.

To run an image inside of a container, we use the docker run command. It requires one parameter and that is the image name. Let’s start our image and make sure it is running correctly. Execute the following command in your terminal.

When you run this command, you’ll notice that you were not returned to the command prompt. This is because our application is a REST server and will run in a loop waiting for incoming requests without return control back to the OS until we stop the container.

Let’s make a GET request to the server using the curl command.

Our curl command failed because the connection to our server was refused. Meaning that we were not able to connect to localhost on port 8080. This is expected because our container is running in isolation which includes networking. Let’s stop the container and restart with port 8080 published on our local network.

To stop the container, press ctrl-c. This will return you to the terminal prompt.

Start the container and expose port 8080 to port 8080 on the host.

Now let’s rerun the curl command from above.

Success! We were able to connect to the application running inside of our container on port 8080. Switch back to the terminal where your container is running and you should see the GET request logged to the console.

Press ctrl-c to stop the container.

Run in detached mode

Docker started our container in the background and printed the container ID on the terminal.

Again, let’s make sure that our container is running properly. Run the same curl command:

List containers

Since we ran our container in the background, how do we know if our container is running or what other containers are running on our machine? Well, we can run the docker ps command. Just like on Linux, to see a list of processes on your machine we would run the ps command. In the same spirit, we can run the docker ps command which will show us a list of containers running on our machine.

The ps command tells a bunch of stuff about our running containers. We can see the container ID, the image running inside the container, the command that was used to start the container, when it was created, the status, ports that are exposed, and the name of the container.

You are probably wondering where the name of our container is coming from. Since we didn’t provide a name for the container when we started it, Docker generated a random name. We’ll fix this in a minute but first we need to stop the container. To stop the container, run the docker stop command which does just that, stops the container. You will need to pass the name of the container or you can use the container ID.

Now rerun the docker ps command to see a list of running containers.

Stop, start, and name containers

If you’ve been following along, you should see several containers listed. These are containers that we started and stopped but have not removed yet.

Let’s restart the container that we have just stopped. Locate the name of the container and replace the name of the container below in the restart command:

Now, list all the containers again using the ps command:

Notice that the container we just restarted has been started in detached mode and has port 8080 exposed. Also, note that the status of the container is “Up X seconds”. When you restart a container, it will be started with the same flags or commands that it was originally started with.

Let’s stop and remove all of our containers and take a look at fixing the random naming issue.

Stop the container we just started. Find the name of your running container and replace the name in the command below with the name of the container on your system:

Now that all of our containers are stopped, let’s remove them. When a container is removed, it is no longer running nor is it in the stopped state. Instead, the process inside the container is terminated and the metadata for the container is removed.

To remove a container, run the docker rm command passing the container name. You can pass multiple container names to the command in one command.

Again, make sure you replace the containers names in the below command with the container names from your system:

Now let’s address the pesky random name issue. Standard practice is to name your containers for the simple reason that it is easier to identify what is running in the container and what application or service it is associated with. Just like good naming conventions for variables in your code makes it simpler to read. So goes naming your containers.

Now, we can easily identify our container based on the name.

Next steps

In this module, we learned how to run containers and publish ports. We also learned to manage the lifecycle of containers. We then discussed the importance of naming our containers so that they are more easily identifiable. In the next module, we’ll learn how to run a database in a container and connect it to our application. See:

Feedback

Help us improve this topic by providing your feedback. Let us know what you think by creating an issue in the Docker Docs GitHub repository. Alternatively, create a PR to suggest updates.

Docker — Beginner’s Guide — Part 1: Images & Containers

Welcome to this new CodingTheSmartWay.com tutorial on getting started with Docker. Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. This is often described as containerization. Putting applications into containers leads to several advantages:

Containers vs. Virtual Machines

When talking about containerization it is very often compared to virtual machines. Let’s take a look at the following image to see the main difference:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

The Docker container platform is always running on top of the host operating system. Containers are containing the binaries, libraries, and the application itself. Containers do not contain a guest operating system which ensures that containers are lightweight.

In contrast virtual machines are running on a hypervisor (responsible for running virtual machines) and include it’s own guest operating system. This increased the size of the virtual machines significantly, makes setting up virtual machines more complex and requires more resources to run each virtual machine.

Concept

This tutorial is about Docker and getting started with this popular container platform. Before actually starting to apply Docker in practice let us first clarify some of the most important concepts and terminologies.

Images

A Docker image is containing everything needed to run an application as a container. This includes:

The image can then be deployed to any Docker environment and executable as a container.

Containers

A Docker container is a runtime instance of an image. From one image you can create multiple containers (all running the sample application) on multiple Docker platform.

A container runs as a discrete process on the host machine. Because the container runs without the need to boot up a guest operating system it is lightweight and limits the resources (e.g. memory) which are needed to let it run.

Installation

First of all you need to make sure that Docker is installed on your system. For the following tutorial we’ll assume that the Docker Community Edition (CE) is installed. This edition is ideal for developers looking to get started with Docker and experimenting with container-based apps — so this is the perfect choice for our use case.

Docker CE is available for all major platforms including MacOS, Windows and Linux. The specifc steps needed to install Docker CE on your system can be found at https://docs.docker.com/install/.

For the following tutorial I’ll use Docker Desktop for Mac. The detailed installation instructions for this version can be found at https://docs.docker.com/docker-for-mac/install/.

Furthermore you should make sure to create a free account at https://hub.docker.com, so that you can use this account to sign in in the Docker Desktop application.

Finally you should be sure that the Docker Desktop application is started.

Once Docker is installed and running on your system we’re able to start by entering the following command on the terminal:

This command will output a list with all options available for the docker command together with a short description. As output you should be able to see something similar to the following:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Next, use the following command to check for the installed Docker version:

The output gives you detailed information about the installed version of Docker:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Selecting An Image

Now that Docker is up and running we’re ready to select an image so that we’re able to run our first docker container. To select from the list of already existing Docker images go to hub.docker.com:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Make sure that you’re logged in with your Docker Hub account and then use the search field to enter a search term which is matching the name of the application for which you’d like to find an existing Docker image.

Let’s say that we’d first like to run a Nginx web server. Type in the search term “nginx”. A result list will be presented:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

The first result is the official Nginx image named nginx, so this is exactly what we’re looking for. Click on the image to by able to see the details page:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

On the details page you can find an overview of the versions of this image and find links to the corresponding Dockerfiles.

A Dockerfile is a text document that contains all the commands you would normally execute manually in order to build a Docker image. Docker can build images automatically by reading the instructions from a Dockerfile.

Later we’ll go through the process of writing an Dockerfile from scratch.

Working With Images & Containers

In the last step we’ve used Docker Hub to search for docker images. Let’s run the nginx image by using the following command:

The docker run command lets you run any Docker image as an container. In this example we’re using the following options in addition:

The name of the Docker image we’d like to use for starting up the container needs to be supplied as well, in out case nginx.

The response which is displayed in the browser when accessing the container by URL http://localhost:80 in the browser should like what you can see in the following screenshot:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Because we’re running the container in interactive mode you should be able to see the log output of the incoming HTTP GET request on the command line:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Next, on a second terminal instance, we’re able to check for available containers by typing in the following command:

The output will show you one result:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Here you can see that the container based on the nginx image is now running and is identified by a unique ID.

A container can be deleted by using the following command:

$ docker rm [CONTAINER_ID]

However, if we try to delete our Nginx container Docker is telling us that it’s not possible to delete a running running. Because we’re still running the container in interactive mode it’s easy to stop the container by just pressing CTRL+C in the terminal instance in which the container has been started. Now it is possible to delete the container by using the docker rm command.

Maybe you’ve noticed that the image for our container ( nginx) was downloaded to our system when the container was started. Having deleted the container now the nginx image should still be available on our system. You can check this by using the following command:

And you’ll see from the result that the nginx image is available on the system:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Deleting an image from the system is done by using the following command:

$ docker images rm [IMAGE_ID]

If you only want to download an image from Docker Hub to the system (without running an container) you can also use the docker pull command:

$ docker pull nginx

Because the image is then available on your system creating containers based on that image is faster because the image file needs not to be downloaded first.

Running Containers In Detached Mode

$ docker stop [CONTAINER_ID]

You can also get an overview of containers which have already been stopped by using command:

Assigning Container Names

Up until now we’ve used the container ID to identifier and refer to a container. Also the container gets assigned a unique name which can be used to refer

If you now print our the list of running containers with command docker ps you can see that the container has been created with the name specified:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

All the commands which we’ve used so far together with a container ID can be used with the container name as well, e.g. to stop the running container with name mynginx01 you can use the following command:

$ docker stop mynginx01

Restarting Containers

If you’d like to restart any container which is in state excited you can use the name of that container or the ID of that container together with the docker start command like you can see in the following:

$ docker start mynginx01

Running A Command In A Running Container

Now that we have a running container we need to find ways to interact with the container and the application which is running inside of the container.

First of all lets see how we can run command inside of the running container. This is done by using the docker exec command. In the following example we’re using that command to start a bash terminal for our running container myngin01.

The command prompt will then be switched to the bash shell which is running in the container and you’re able to execute further command. For example you can enter the html directory of the Nginx web server by using the following command:

Inside this folder you’ll find a file index.html which contains the HTML code which is used to output the default Nginx page in the browser. To be able to see the HTML content of the file you can use the cat command:

The output should then look like the following:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Sharing Data BetWeen The Docker Container And The Host

We’ve been able so explore the Docker container in the last step and see from where the HTML content is coming which is displayed in the browser when accessing the Nginx web server.

This leads to the question: how can we put our own content into the container, so that Nginx is delivering the content we’re providing. The answer to this question is: a bindmount volume needs to be used to link a directory from inside the Nginx container ( usr/share/nginx/html/) to a directory on the host machine. This enables us to store HTML content which should be delivered by the Nginx web server on the host machine. Let’s see how that works:

First let’s create a new empty directory on the host machine which then should contain the HTML files:

Change into the newly created empty folder:

And create a new file index.html inside of that folder:

Open this file in your favourite text editor and insert the following HTML code:

This is just a very simple HTML file outputting a headline with a paragraph of text. Let’s start the Nginx container once again in the following way:

When accessing the server in the browser again you should then be able to see the output which is coming from our new file index.html which is now made available within the container and served by Nginx:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Let’s explore another use case which can be used to share data between host and container.

The Nginx server is creating log files. These log files are created inside the container directory /var/log/nginx. To make the content of this folder available we’re able to mount it to a host path as well:

The container is now running again and we should be able to see the same result in the browser when accessing port 80 on our local machine. Furthermore we’re now able to take a look inside of

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Building New Docker Images

So far we’ve been using pre-build Docker images to run our containers. Docker makes it very easy to build your own images and therewith extend or adapt existing images in many ways.

In this section we’re going to explore how custom Docker images can be build by using a Dockerfile. The following definition from the official Docker documentation summerizes what a Dockerfile is:

A Dockerfile is a text document that contains all the commands you would normally execute manually in order to build a Docker image. Docker can build images automatically by reading the instructions from a Dockerfile.

So let’s see how we can make use of a Dockerfile to create a new image (based on the official nginx image) and make sure that this new image at the same time is containing or own HTML files in the web server’s html folder.

First let’s create a new file Dockerfile in the directory in which the html directory is contained:

Open up the Dockerfile in your text editor and insert the following two lines:

By using the FROM command in the first line we’re declaring that the new image should be based on the nginx image.

The COPY command is used in the second line to copy the content from the html directory of the host system to the directory /usr/share/nginx/html of the container to be created.

To create the new image based on the Dockerfile you need to execute the following command inside the directory in which the Dockerfile is located:

This command is creating a new image with name mynginx_image1:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Having created this image we’re now ready to start a new container based on this newly created image by using the docker run command once again in the following way:

Now, when accessing the Nginx web server again in the browser we’re receiving the same output as before (which is coming from our custom index.html file). This file is already stored inside this container because it has been build into our image by using the Dockerfile from above.

Share Your Image On Docker Hub

We’ve already visited hub.docker.com to search for images which we can be use to run containers. Now that we’ve managed to build our own custom image let’s see how we can use Docker Hub to upload and share our image.

First you need to create a free account on Docker Hub and make sure that you’re logged in with this account in your Docker Desktop application.

You can also use the docker command on the command line to login by using

Now we need to create an additional tag for our image which should be pushed to Docker Hub in the next step. The general syntax to create a new tag looks like the following:

$ docker tag [image] [username/repository:tag]

In my case I’m using the command in the following way:

$ docker tag mynginx_image1 codingthesmartway/mynginx_image1

Here I’m using my Docker Hub username codingthesmartway and I’m telling Docker to create a new tag with the name c odingthesmartway/mynginx_image1.

In the last step I’m now able to use the docker push command to actually push my image to Docker Hub and make it available publicly, so that it can be used anywhere:

$ docker push codingthesmartway/mynginx

The output on the command line should look like you can see in the following screenshot:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Once complete, the results of this upload are publicly available. If you log in to Docker Hub, you can see the new image:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Top 3 Docker Online Courses

Disclaimer: This post contains affiliate links, which means that if you click on one of the product links, I’ll receive a small commission. This helps support this blog!

How to Use Docker Run Command with Examples

Home » DevOps and Development » How to Use Docker Run Command with Examples

One of the first and most important commands Docker users learn is the docker run command. This comes as no surprise since its primary function is to build and run containers.

There are many different ways to run a container. By adding attributes to the basic syntax, you can configure a container to run in detached mode, set a container name, mount a volume, and perform many more tasks.

In this tutorial, you will learn how to use docker run commands with examples.

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Note: You can use these commands as the root user or as a user with sudo privileges, in which case you must add the sudo prefix.

How to Use the docker run Command

The basic syntax for the command is:

To run a container, the only thing you need to include in the command is the image on which it is based:

You can run containers from locally stored Docker images. If you use an image that is not on your system, the software pulls it from the online registry.

The command to run our sample container would be:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

The container performs the prescribed task (echoes the message Hello World ) and then stops.

Note: With the release of Docker 1.13, Docker introduced a new CLI in which it regrouped commands according to the object they interact with. Accordingly, run is now a subcommand of docker container and to use it you must type docker container run. Although Docker still supports docker run, it recommends getting use to the new syntax.

Run a Container Under a Specific Name

When you use the basic run command, Docker automatically generates a container name with a string of randomly selected numbers and letters.

Since there is a slim chance you will be able to remember or recognize the containers by these generic names, consider setting the container name to something more memorable.

Using the —name attribute allows you to assign a container name. The command for running a container under a specific name is:

For instance, we can run the sample container and give it the name container_instance using the command:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

You can check whether you have successfully set a container name by displaying a list of all containers (running and stopped) with the command:

As in the image below, you should see your newly created container.

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Run a Container in the Background (Detached Mode)

There are two ways of running a container – in attached mode (in the foreground) or in detached mode (in the background).

By default, Docker runs the container in attached mode. Meaning it’s attached to the terminal session, where it displays output and messages.

If you want to keep the container and current terminal session separate, you can run the container in the background using the -d attribute. Using detached mode also allows you to close the opened terminal session without stopping the container.

The command for running a container in the background is:

For our example, the command is:

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

The output you receive will be similar to the one you see in the image above. The container will run the process and then stop. No other output will display inside the terminal session.

Note: Running Docker privileged containers is also one of the most commonly used run commands. However, did you know that it is not advisable to use privileged containers due to a potential threat to the system?

Run a Container Interactively

Docker allows you to run a container in interactive mode. This means you can execute commands inside the container while it is still running.

By using the container interactively, you can access a command prompt inside the running container. To do so, run the following command:

The command prompt will change, moving you to the bash shell as in the example below.

How to run docker image. Смотреть фото How to run docker image. Смотреть картинку How to run docker image. Картинка про How to run docker image. Фото How to run docker image

Run a Container and Publish Container Ports

When you run a container, the only way to access the process is from inside of it. To allow external connections to the container, you have to open (publish) specific ports.

You have to add the -p option to the docker run command as well as the following information:

The host_ip element is optional and you don’t need to specify it when running the command.

For example, to map TCP port 80 in the container to port 8080 on the Docker host you would run:

Run a Container and Mount Host Volumes

Docker containers do not save the data they produce. As soon as the process is finished, the container stops and everything inside of it is removed.

If you want to have persistent data that is stored even after the container stops, you need to enable sharing storage volumes.

For mounting volumes use the -v attribute with the specified location of the directory where you want to save the data, followed by where that data will be located inside the container.

The entire docker container run command is:

Run a Docker Container and Remove it Once the Process is Complete

Once a container executes its tasks, it stops, but the file system it consists of remains on the system.

If you only need a container to execute the outlined task and have no use of it or its file system afterward, you can set it up to delete once it is done.

To run a container that will be automatically removed after exiting use the command:

Note: Want to remove other unnecessary containers from the system? Check out How to Remove Docker Containers, Images, Networks & Volumes.

Docker has earned a prominent place in development trends. The sooner you get acquainted with its command line interface the better. The first step is mastering the docker run command.

You can use this guide to learn the basics or as useful reminder when needed.

Источники информации:

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *