Docker Basics (Part 2) : Essential Container Options

In the dynamic world of Docker, mastering various options is key to unlocking the full potential of containerization. Let's dive into some fundamental Docker options that shape the behavior and functionality of your containers.

Complete Docker Series : View Series

Interactive (-it)

  • Runs in an interactive mode
# Runs bash in ubuntu
docker run -it ubuntu /bin/bash
  • -i: Keeps STDIN open, allowing you to provide input to the container.

  • -t: Allocates a pseudo-TTY, which makes the interaction with the command-line

Detached (-d)

# Runs nginx server in the background
docker run -d -p 80:80 nginx
  • -d: Run the container in the background (detached mode).

  • -p 80:80: Map port 80 on the host to port 80 on the container.

Port mapping (-p)

# Maps host port 8080 to container port 80 for an Apache web server
docker run -d -p 8080:80 httpd
  • -p 8080:80: Maps host port 8080 to container port 80.

  • <host>:<container>

Container naming (--name)

# Run a PostgreSQL container with a custom name
docker run --name mypostgres postgres
  • --name mypostgres: Assigns the name "mypostgres" to the container.

Environment variables (-e)

# Sets the enviornment variable
docker run -e POSTGRES_PASSWORD=pass123 postgres
  • -e POSTGRES_PASSWORD=pass123: Sets the PostgreSQL password using an environment variable.

  • POSTGRES_PASSWORD environment variable is used by the PostgreSQL container to set the password for the default postgres user during initialization.

Volume mounting (-v)

docker run -v /path/on/host:/path/in/container nginx
  • Mount a local directory into a container for persistent storage:

  • -v /path/on/host:/path/in/container: Mounts the local directory into the container.

Working directory (-w)

# Run a Node.js application, setting the working directory
docker run -w /usr/src/app node npm start
# docker run -w <directory location> <container or image> <command>
  • Runs npm start in /usr/src/app in a node conainer

Network (--network)

docker run --network host <image>
  • default is bridge

  • other than host, can give none and also custom ones

Did you find this article valuable?

Support Aswin Benny by becoming a sponsor. Any amount is appreciated!