Docker Run Command with Examples

Docker Run Command with Examples

“Docker Run Command with Examples”

We hope this post helped you to find out  “Docker Run Command with Examples”

Docker is a platform that means that you can develop, take a look at, and deploy purposes as transportable, self-sufficient containers that run nearly wherever.

The docker run command creates a container from a given picture and begins the container utilizing a given command. It is without doubt one of the first instructions it’s best to grow to be acquainted with when beginning to work with Docker.

On this article, we’ll use the official Nginx picture to point out numerous methods to run a Docker container.

Docker Run Command #

The docker run command takes the next type:

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

The title of the picture from which the container must be created is the one required argument for the docker run command. If the picture is just not current on the native system, it’s pulled from the registry.

If no command is specified, the command specified within the Dockerfile’s CMD or ENTRYPOINT directions is executed when working the container.

Ranging from model 1.13, the Docker CLI has been restructured, and all instructions have been grouped underneath the article they interacting with.

For the reason that run command interacts with containers, now it’s a subcommand of docker container. The syntax of the brand new command is as follows:

docker container run [OPTIONS] IMAGE [COMMAND] [ARG...]

The outdated, pre 1.13 syntax remains to be supported. Below the hood, docker run command is an alias to docker container run. Customers are inspired to make use of the brand new command syntax.

A listing of all docker container run choices might be discovered on the Docker documentation web page.

Run the Container within the Foreground #

By default, when no choice is supplied to the docker run command, the basis course of is began within the foreground. Because of this the usual enter, output, and error from the basis course of are connected to the terminal session.

docker container run nginx

The output of the nginx course of will likely be displayed in your terminal. Since, there are not any connections to the webserver, the terminal is empty.

To cease the container, terminate the working Nginx course of by urgent CTRL+C.

Run the Container in Indifferent Mode #

To maintain the container working if you exit the terminal session, begin it in a indifferent mode. That is just like working a Linux course of within the background.

Use the -d choice to start out a indifferent container:

docker container run -d nginx

The indifferent container will cease when the basis course of is terminated.

You possibly can checklist the working containers utilizing the docker container ls command.

To connect your terminal to the indifferent container root course of, use the docker container attach command.

Take away the Container After Exit #

By default, when the container exits, its file system persists on the host system.

The --rm choices tells docker run command to take away the container when it exits robotically:

docker container run --rm nginx

The Nginx picture might not be the very best instance to scrub up the container’s file system after the container exits. This feature is normally used on foreground containers that carry out short-term duties similar to exams or database backups.

Set the Container Identify #

In Docker, every container is recognized by its UUID and title. By default, if not explicitly set, the container’s title is robotically generated by the Docker daemon.

Use the --name choice to assign a customized title to the container:

docker container run -d --name my_nginx nginx

The container title have to be distinctive. In the event you attempt to begin one other container with the identical title, you’ll get an error just like this:

docker: Error response from daemon: Battle. The container title "/my_nginx" is already in use by container "9...c". You need to take away (or rename) that container to have the ability to reuse that title.

Run docker container ls -a to checklist all containers, and see their names:

docker container ls
9d695c1f5ef4  nginx  "nginx -g 'daemon of…"  36 seconds in the past  Up 35 seconds  80/tcp  my_nginx

The significant names are helpful to reference the container inside a Docker community or when working docker CLI instructions.

Publishing Container Ports #

By default, if no ports are revealed, the method working within the container is accessible solely from contained in the container.

Publishing ports means mapping container ports to the host machine ports in order that the ports can be found to companies outdoors of Docker.

To publish a port use the -p choices as follows:

-p host_ip:host_port:container_port/protocol
  • If no host_ip is specified, it defaults to
  • If no protocol is specified, it defaults to TCP.
  • To publish a number of ports, use a number of -p choices.

To map the TCP port 80 (nginx) within the container to port 8080 on the host localhost interface, you’d run:

docker container run --name web_server -d -p 8080:80 nginx

You possibly can confirm that the port is revealed by opening http://localhost:8080 in your browser or working the next curl command on the Docker host:

curl -I http://localhost:8080

The output will look one thing like this:

HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Tue, 26 Nov 2019 22:55:59 GMT
Content material-Kind: textual content/html
Content material-Size: 612
Final-Modified: Tue, 19 Nov 2019 12:50:08 GMT
Connection: keep-alive
ETag: "5dd3e500-264"
Settle for-Ranges: bytes

Sharing Knowledge (Mounting Volumes) #

When a container is stopped, all information generated by the container is eliminated. Docker Volumes are the popular technique to make the information persist and share it throughout a number of containers.

To create and handle volumes, use the -p choices as follows:

-v host_src:container_dest:choices
  • The host_src might be an absolute path to a file or listing on the host or a named quantity.
  • The container_dest is an absolute path to a file or listing on the container.
  • Choices might be rw (read-write) and ro (read-only). If no choice is specified, it defaults to rw.

To clarify how this works, let’s create a listing on the host and put an index.html file in it:

mkdir public_htmlecho "Testing Docker Volumes" > public_html/index.html

Subsequent, mount the public_html listing into /usr/share/nginx/html within the container:

docker run --name web_server -d -p 8080:80 -v $(pwd)/public_html:/usr/share/nginx/html nginx

As a substitute of specifying absolutely the path to the public_html listing, we’re utilizing the $(pwd) command, which prints the present working listing.

Now, for those who sort http://localhost:8080 in your browser, it’s best to see the contents of the index.html file. You too can use curl:

curl http://localhost:8080
Testing Docker Volumes

Run the Container Interactively #

When coping with the interactive processes like bash, use the -i and -t choices to start out the container.

The -it choices tells Docker to maintain the usual enter connected to the terminal and allocate a pseudo-tty:

docker container run -it nginx /bin/bash

The container’s Bash shell will likely be connected to the terminal, and the command immediate will change:


Now, you may work together with the container’s shell and run any command within it.

On this instance, we supplied a command (/bin/bash) as an argument to the docker run command that was executed as a substitute of the one specified within the Dockerfile.

Conclusion #

Docker is the usual for packaging and deploying purposes and an integral part of CI/CD, automation, and DevOps.

The docker container run command is used to create and run Docker containers.

We hope the “Docker Run Command with Examples” help you. If you have any query regarding “Docker Run Command with Examples” drop a comment below and we will get back to you at the earliest.

We hope this post helped you to find out  “Docker Run Command with Examples”. You may also want to see – How to Set Up a Firewall with UFW on Ubuntu 20.04

Share via
Copy link
Powered by Social Snap