
- Docker - Home
- Docker - Overview
- Docker - Installing on Linux
- Docker - Installation
- Docker - Hub
- Docker - Images
- Docker - Containers
- Docker - Registries
- Docker - Compose
- Docker - Working With Containers
- Docker - Architecture
- Docker - Layers
- Docker - Container & Hosts
- Docker - Configuration
- Docker - Containers & Shells
- Docker - Dockerfile
- Docker - Building Files
- Docker - Public Repositories
- Docker - Managing Ports
- Docker - Web Server
- Docker - Commands
- Docker - Container Linking
- Docker - Data Storage
- Docker - Volumes
- Docker - Networking
- Docker - Security
- Docker - Toolbox
- Docker - Cloud
- Docker - Build Cloud
- Docker - Logging
- Docker - Continuous Integration
- Docker - Kubernetes Architecture
- Docker - Working of Kubernetes
- Docker - Generative AI
- Docker - Hosting
- Docker - Best Practices
- Docker - Setting Node.js
- Docker - Setting MongoDB
- Docker - Setting NGINX
- Docker - Setting ASP.Net
- Docker - Setting MySQL
- Docker - Setting Go
- Docker - Setting Rust
- Docker - Setting Apache
- Docker - Setting MariaDB
- Docker - Setting Jupyter
- Docker - Setting Portainer
- Docker - Setting Rstudio
- Docker - Setting Plex
- Docker Setting - Flame
- Docker Setting - PostgreSql
- Docker Setting - Mosquitto
- Docker Setting - Grafana
- Docker Setting - Nextcloud
- Docker Setting - Pawns
- Docker Setting - Ubuntu
- Docker Setting - RabbitMQ
- Docker - Setting Python
- Docker - Setting Java
- Docker - Setting Redis
- Docker - Setting Alpine
- Docker - Setting BusyBox
- Docker Setting - Traefik
- Docker Setting - WordPress
- Docker Useful Resources
- Docker - Quick Guide
- Docker - Useful Resources
- Docker - Discussion
Docker - Working with Containers
A Docker Container is a portable software unit that packages up an application and all its dependencies to run on any infrastructure. The application, hence runs consistently in many development, testing, and production environments. This chapter focuses on some basic commands for Docker containers which will help you to seamlessly work with containers.
Important Docker Container Commands
Docker ships with many commands that can be used to build, manage, and run containers. These commands are fundamental in working with Docker; they empower you to control both the life cycle and behavior of containers in a very efficient way. Knowing and mastering the use of these commands is crucial to leverage Docker effectively.
Creating and Starting Containers - docker run command
The docker run command creates a new container and starts it. It includes the functionalities of pulling an image from a repository (if it's not already on your local system), creating a container, and starting one. Several options and flags can be passed to alter the container's behavior, like setting environment variables, mounting volumes, and configuring networking.
Command
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
Example
$ docker run -d -p 80:80 --name mywebserver nginx

This command will launch our nginx web server container in detached mode (-d), expose port 80 of the host to port 80 of the container (-p 80:80), and name the container my webserver.
Listing Containers
The docker ps command is used to list all running containers. By default, it only lists the running containers, but one can provide extra flags to include stopped containers or filter the results based on arbitrary criteria. This command outputs vital information such as container IDs, names, statuses, and the command that started a particular container.
Command
$ docker ps
Example
$ docker ps -a

This command lists all containers, including those that are stopped (-a).
Managing Container States
These are the commands used to control the states of containers.
- Docker Start − Start one or more stopped containers.
- Docker Stop − Stop running containers gracefully to have time for their correct shutdown.
- Docker Restart − Stops and then starts one or more containers, effectively rebooting them.
Commands
$ docker start [CONTAINER] $ docker stop [CONTAINER] $ docker restart [CONTAINER]
Examples
$ docker start mywebserver $ docker stop mywebserver $ docker restart mywebserver

The above commands, respectively start, stop, and restart a container named my webserver.
Viewing Container Logs
One can use the docker logs command to extract the logs from a running or stopped container. It will provide a view of the container's output and its behavior, which can be very helpful for debugging and monitoring purposes. You can also follow real-time logs or limit the output to recent log entries.
Command
$ docker logs [OPTIONS] CONTAINER
Example
$ docker logs mywebserver

This command displays the logs of the mywebserver container.
Removing Containers
The docker rm command removes one or more containers. It is useful for cleaning up containers that are no longer needed. To remove a running container, you must stop it first or use the -f (force) option to forcibly remove it.
Command
$ docker rm [OPTIONS] CONTAINER [CONTAINER...]
Example
$ docker rm mywebserver

This command removes the mywebserver container.
Creating Images from Containers
The docker commit command commits a new image based on changes from a container. It's useful when you want to save the state of a container at the current moment in time and then share that state with someone or use it again in another container.
Command
$ docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
Example
$ docker commit mywebserver mynewimage

This command will create a new image from the webserver container, mynewimage.
Other Key Commands
Docker also has several other commands to work with and manage containers.
- Docker exec − Execute a command in an active container.
- Docker cp − Copy files/folders between a container and the local filesystem.
- Docker top − Display the running processes of a container.
- Docker attach − Attach to a running container to interact with it.
- Docker pause and unpause − These are for pausing the container process and resuming it, respectively.
Commands and Examples
The below command runs a Bash shell in the mywebserver container interactively.
$ docker exec [OPTIONS] CONTAINER COMMAND [ARG...] $ docker exec -it mywebserver /bin/bash
The next command copies the contents of /var/www/html from the mywebserver container to the local ./local_copy directory.
$ docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- $ docker cp mywebserver:/var/www/html ./local_copy
The below command displays the running processes of the mywebserver container.
$ docker top CONTAINER [ps OPTIONS] $ docker top mywebserver

The next command attaches your terminal to the mywebserver container.
$ docker attach [OPTIONS] CONTAINER $ docker attach mywebserver

The subsequent commands pause and unpause the mywebserver container, respectively.
$ docker pause CONTAINER [CONTAINER...] $ docker unpause CONTAINER [CONTAINER...] $ docker pause mywebserver $ docker unpause mywebserver

Publishing Container Ports
Publishing of container ports makes container services accessible from outside the Docker host. By mapping container ports to host ports, you can expose the services running inside the container to external clients.
Command
$ docker run -p HOST_PORT:CONTAINER_PORT [OPTIONS] IMAGE [COMMAND] [ARG...]
Example
$ docker run -d -p 8080:80 --name webserver nginx

The above command will run an nginx container and map the port 80 of the container to the port 8080 on the host, allowing the webserver to be accessed at http://localhost:8080.
Resource Management (CPU, Memory, IO)
Docker offers several capabilities for setting limits and managing the resources allocated to a container. This safeguards against single-container resource utilization in an unfair manner.
CPU Limits
$ docker run --cpus="1.5" [OPTIONS] IMAGE [COMMAND] [ARG...]
This command restricts the container to use a maximum of 1.5 CPU cores.
Memory Limits
$ docker run --memory="512m" [OPTIONS] IMAGE [COMMAND] [ARG...]
This command limits the container to a maximum of 512 MB of memory.
IO Limits
$ docker run --blkio-weight=500 [OPTIONS] IMAGE [COMMAND] [ARG...]
This command initializes the block IO weight for the container; the user can influence the IO priority.
Conclusion
Docker has redefined the landscape of software development and deployment with the support of a powerful containerization platform. It's important to understand and master its essential commands, networking capabilities, advanced operations, and best practices when using Docker to harness its full potential.
As you continue to tinker with and spend time using Docker, these basic and advanced concepts will help enable you to build, manage, and deploy not only scalable, and secure, but also efficient applications.
FAQs on Docker Working with Containers
1. How does Docker isolate containers from each other and the host system?
Namespaces and control groups mainly implement isolation in Docker. Namespaces provide isolation - they allow each container its view of the system with an independent process list, network setup, and file system.
Control groups limit the amount of resources a container can use at once to ensure that it cannot starve the rest of the system in cases of heavy loads in terms of CPU, memory, or I/O. Such isolation ensures that the contained application can run independently without interfering with other applications or the underlying host.
2. Can I use Docker on a production server?
Yes, it's important to emphasize that Docker is heavily used in production environments. Organizations run services in containers to package and deploy applications onto different platforms reliably.
Be sure to follow best practices for security, monitoring, and scalability as you come into production. You can use orchestration tools like Docker Swarm or Kubernetes to help manage large-scale Docker deployments.
3. How do I update a Docker container without losing its data?
To update a container non-destructively, you first stop the container. You pull an updated image from the registry, and after pulling an updated image from the registry, you remove the old existing container - preserving its volumes with the "-v" flag on docker rm.
Finally, use the updated image to start a new container and mount the same volumes in the previous one. This way, your data will still be consistent after the update.