In the previous post we’ve learnt some of the most useful Docker Images commands. Now, it’s time to start working with running images, or, in other words Containers.
Docker Container is essentially a running application of some sort, for example C#, Python or just a simple Bash. All you need to do to run container is to have an image or pull your desired image from Container Registry and then run it in your Docker environment.
Create Container
Let’s start with creating our first container using the docker create command.
More details: docker create cli
Create Container
docker create --name [CONTAINER NAME] [IMAGE NAME]:[TAG]
Create Container with Flags
docker create [FLAG] [IMAGE NAME]:[TAG]
Flags
–rm | Automatically remove container once stopped |
-it | Allocates command line to container’s STDIN |
-d | Run container in detached mode, ie. background |
-v | Mount a volume Example with new volume: -v /local-folder:/container-folder Example with existing volume: -v [VOLUME NAME]:/container-folder |
-p | Binds port from container to local env Example: -p local-port:container-port |
List Containers
To check what containers you have running (either stopped or started state) in your system, use the ps command.
More details: docker ps cli
List Running Containers Only
docker ps
List Running & Stopped Containers
docker ps -a
Start Container
To start stopped instance of a container use the start command.
More details: docker start cli
Start Container
docker start [CONTAINER NAME]
Start Container and Attach STDIN
docker start -a [CONTAINER NAME]
Restart Container
To restart running instance of a container use the restart command.
More details: docker restart cli
docker restart [CONTAINER NAME]
Exec Container
To enter running instance of a container use the exec command.
More details: docker exec cli
Execute Command on Container
docker exec -d [CONTAINER NAME] bash [COMMAND]
Execute into Container
docker exec -it [CONTAINER NAME] bash [COMMAND]
Stop Container
To stop running instance of a container use the stop command.
More details: docker stop cli
docker stop [CONTAINER NAME]
Remove Container
Sometimes you might want to remove unused containers to save disk space or to de-clutter your Docker environment. Docker rm command serves that purpose.
More details: docker rm cli
Remove Container
docker rm [CONTAINER NAME]
Force Remove Container
docker rm -f [CONTAINER NAME]
Remove Container plus Remove its Volume
docker rm -v [CONTAINER NAME]
<< Docker Images | Docker Containers | Docker Volumes >>