|
Home > Wiki > Docker Articles
All articles are licensed under CC0 1.0 making them public domain.
Starting, stopping and deleting contains #
Last updated February 2021
Most common commands I use to manage Docker containers.
Starting a new Docker container
docker run -d --restart unless-stopped -v $PWD/x:/y -p HOST_PORT:CONTAINER_PORT(/tcp|udp) username/imgname
-d
Run in detached mode with no CLI that continues running when terminal closes.
--restart unless-stopped
Restart the container if Docker closes unless container has been manually stopped.
Restart Policies
no - do not automatically restart the container (the default)
on-failure - restart the container if it exits due to an error, which manifests as a non-zero exit code
always - always restart the container if it stops. If it is manually stopped, it is restarted only when Docker daemon restarts or the container itself is manually restarted
unless-stopped - (my most used) similar to always, except that when the container is stopped (manually or otherwise), it is not restarted even after Docker daemon restarts
-v $PWD/x:/y
Map a local file/directory to one within container. Both directories must be absolute hence why $PWD is present and non-optional.
-p HOST_PORT:CONTAINER_PORT(/tcp|udp)
Map a container port to the host. Specifying TCP/UDP is optional and is TCP by default.
username/imgname
The local or external Docker image.
List running containers
docker ps
List all containers
docker ps -a
List only IDs (for scripts)
docker ps -q
Stop a Docker Container
Docker will call SIGTERM and then after a grace period, SIGKILL
docker stop [container id]
Delete Single Container
docker rm [container id]
Delete all Containers
docker rm `docker ps -aq`
Docker based development #
Last updated February 2021
I believe this to be an insane idea and should not be tried, though I keep it here for prosperity. 2026/01/29
Method of spinning up a Docker container for each project instead of installing the toolchain on the host computer.
Why not a Dockerfile
Dockerfiles are the preferred method for deploying apps, their only limitation is that they produce a static image which isn't ideal for rapid development.
This method takes the best of both worlds when deploying with Docker or developing on your local machine: a clean environment with an already configured toolchain and a live shell for development.
Don't be mistaken: this method isn't for deployment, only for development.
Method Outline
- Start a new Docker container with a toolchain-specific image while binding the directory and ports
- Run a new shell within the container from your terminal
- Additional setup, e.g. installing package or external dependencies
- Running the command(s) to start the app
Starting a new container with a shell
In this example a Jekyll app is used. Ruby's toolchain is generally annoying to configure but using Docker containers can make it easier:
docker run -it --name moodboard -p 3000:80 -v "$PWD/moodboard":/usr/app ruby bash
-it
Keep the container interactive so we can use a shell. In Docker's words:
-i, Keep STDIN open even if not attached
-t, Allocate a pseudo-TTY
--name moodboard
The name of the Docker container.
-p 3000:80
Bind the container's port 80, to the host's port 3000.
-v "$PWD/moodboard":/usr/app
Bind a host's directory to the container's `/usr/app` directory. The container's directory is fully optional, this is where you will `cd` to run your app.
ruby
Run the `ruby:latest` image.
bash
Run a bash shell inside of the container.
Re-attaching to a container
After exiting out of the container for the first time it'll auto shutdown. Start it back up and re-attach bash:
Start it up docker start [name]
Run an interactive shell docker exec -it [name] bash
Optionally stop it when finished docker stop [moodboard]
Non-interactive
In some cases you don't need an interactive shell. Apache is a great example of this, by binding a directory and port you can quickly spin up a local webserver.
The command structure looks similar but without a shell or interactive-based flags. Also check the image's docs to find out which directory the image requires.
docker run --name web -p 3000:80 -v "$PWD/web":/usr/local/apache2/htdocs/ -d httpd
|