Docker Run

Docker Run #

Super simple command to run instances of Docker images.

To run instances of images:

docker run <image>

Version #

To specify a version of an image:

docker run <image>:<version tag>

Default version tag is the latest (<image>: latest).

Inputs #

Inputs can be passed to Docker image runs.

To use inputs, map the standard input with -i, which means “interactive”.

docker run -i <docker app>

^ This would ask for an input in an interactive mode.

To include an input from the terminal/attach to the terminal:

docker run -it <docker app>

Daemons #

Daemons are specified with a -d.

docker run -d

Port mapping #

Running a Docker container makes it accessible through a port and specific IP address.

For instance, running a Docker web app might make it accessible at http://123.123.0.1:5000.

To make a local address accessible externally, will need a local port to be mapped to an externally-accessible port.

docker run -p <target port>:<original port> <docker app image or tag>

For instance:

docker run -p 80:5000 <docker webapp>

Exeternal users can access this app through a specified IP address at port 80. In turn, port 80 access gets routed to port 5000.

Such port mapping allows multiple instances of applications to be run concurrently, each accessible at different ports.

Volume mapping #

This focuses on data persistence (i.e., where are data files or databases persisted?)

Different containers have distinct data containers.

For instance, a MySQL container might be handled as such, but all data would be lost at the end.

docker run mysql
docker stop mysql
docker rm mysql

Alternatively, to persist data beyond the duration of a container, map the internal container data directory to a data directory outside of the cotainer:

docker run -v <directory external to the container>:<relative path to data internal to the container> <image>

This implicitly mounts an external volume to the container internally.

For instance:

docker run -v /opt/datadir:/var/lib/mysql mysql

PS #

To see what Docker images are running:

docker ps

Inspect #

Get extra details of a container beyond a ps with inspect.

docker inspect <container name or id>

Logs #

Inspect logs of a container running in the background:

docker logs <container name or id>