Skip to content

Instantly share code, notes, and snippets.

@dazeb
Last active April 20, 2024 21:50
Show Gist options
  • Save dazeb/5c2f45a62315fe0688cb7380be7da82e to your computer and use it in GitHub Desktop.
Save dazeb/5c2f45a62315fe0688cb7380be7da82e to your computer and use it in GitHub Desktop.
docker container run -it -p 80:80 nginx

Create and run a container in background

docker container run -d -p 80:80 nginx

Shorthand for running a container

docker container run -d -p 80:80 nginx

Naming Containers

docker container run -d -p 80:80 --name nginx-server nginx

Tip: What run did

  • Looked for an image called nginx in the image cache
  • If not found in cache, it looks to the default image repo on Docker Hub
  • Pulled it down (latest version), stored in the image cache
  • Started it in a new container
  • Specified to take port 80 on the host and forward to port 80 on the container
  • Could use docker container run --publish 8000:80 --detach nginx to use port 8000
  • Can specify versions like nginx:1.09

List running containers

docker container ls

or

docker ps

List all containers (Even if not running)

docker container ls -a

Stop container

docker container stop [ID]

Stop all running containers

docker stop $(docker ps -aq)

Remove container (Cannot remove running containers, must stop first)

docker container rm [ID]

To remove a running container use force (-f)

docker container rm -f [ID]

Remove multiple containers

docker container rm [ID] [ID] [ID]

Remove all containers

docker rm $(docker ps -aq)

Get logs (Use name or ID)

docker container logs [NAME]

List processes running in container

docker container top [NAME]

Tip: About Containers

Docker containers are often compared to virtual machines but they are actually just processes running on your host OS. In Windows/Mac, Docker runs in a mini-VM so to see the processes you'll need to connect directly to that. On Linux, however, you can run ps aux and see the processes directly.

Image Commands

List the images we have pulled

docker image ls

We can also just pull down images

docker pull [IMAGE]

Remove image

docker image rm [IMAGE]

Remove all images

docker rmi $(docker images -a -q)

Tip: About Images

  • Images are app binaries and dependencies with metadata about the image data and how to run the image.
  • Images are not a complete OS. No kernel, kernel modules (drivers).
  • The host provides the kernel, a big difference between VMs.

Some sample container creation

NGINX:

docker container run -d -p 80:80 --name nginx nginx

(Note: -p 80:80 is optional as it runs on 80 by default)

APACHE:

docker container run -d -p 8080:80 --name apache httpd

MONGODB:

docker container run -d -p 27017:27017 --name mongo mongo

MYSQL:

docker container run -d -p 3306:3306 --name mysql --env MYSQL_ROOT_PASSWORD=123456 mysql

Container Info

View info on container

docker container inspect [NAME]

Specific property (--format)

docker container inspect --format '{{ .NetworkSettings.IPAddress }}' [NAME]

Performance stats (cpu, mem, network, disk, etc)

docker container stats [NAME]

Accessing Containers

Create new nginx container and bash into

docker container run -it --name [NAME] nginx bash
  • i = interactive: Keep STDIN open if not attached
  • t = tty: Open prompt

For Git Bash, use "winpty"

winpty docker container run -it --name [NAME] nginx bash

Run/Create Ubuntu container

docker container run -it --name ubuntu ubuntu

(Note: no bash because Ubuntu uses bash by default)

You can also make it so when you exit the container does not stay by using the --rm flag

docker container run --rm -it --name [NAME] ubuntu

Access an already created container, start with -ai

docker container start -ai ubuntu

Use exec to edit config, etc

docker container exec -it mysql bash

Alpine is a very small Linux distro good for Docker

docker container run -it alpine sh

(Note: use sh because it does not include bash. Alpine uses apk for its package manager - can install bash if you want)

Limiting The Memory Usage For Containers

In order to limit the amount of memory a Docker container process can use, simply set the -m [memory amount] flag with the limit.

To run a container with memory limited to 256 MBs:

Example:
docker run -m 64m -d -p 8082:80 tutum/wordpress

Networking

"bridge" or "docker0" is the default network

Get port

docker container port [NAME]

List networks

docker network ls

Inspect network

docker network inspect [NETWORK_NAME]

(Note: "bridge" is default)

Create network

docker network create [NETWORK_NAME]

or

docker network create --driver bridge [NETWORK_NAME]

Link to container, add to network

docker run -d --net=[NETWORK_NAME] --name mongodb mongo

Create container on network

docker container run -d --name [NAME] --network [NETWORK_NAME] nginx

Connect existing container to network

docker network connect [NETWORK_NAME] [CONTAINER_NAME]

Disconnect container from network

docker network disconnect [NETWORK_NAME] [CONTAINER_NAME]

Detach network from container

docker network disconnect

Delete/Remove network

To remove the network by name or id, multiple can be deleted:

docker network rm [NETWORK_NAME] [NETWORK_NAME]

Image Tagging & Pushing to Docker Hub

Tags are labels that point to an image ID

docker image ls

You'll see that each image has a tag.

Retag existing image

docker image tag nginx btraversy/nginx

Upload to Docker Hub

docker image push bradtraversy/nginx

If denied, do

docker login

Add tag to new image

docker image tag bradtraversy/nginx bradtraversy/nginx:testing

Dockerfile Parts

  • FROM - The OS used. Common is alpine, debian, ubuntu.
  • ENV - Environment variables.
  • RUN - Run commands/shell scripts, etc.
  • EXPOSE - Ports to expose.
  • CMD - Final command run when you launch a new container from image.
  • WORKDIR - Sets working directory (also could use RUN cd /some/path).
  • COPY - Copies files from host to container.

Build image from Dockerfile (reponame can be whatever)

From the same directory as Dockerfile:

docker image build -t [REPONAME] .

Benchmarking builds

DOCKER_BUILDKIT=1 docker image build -t [REPONAME] .

Tip: Cache & Order

  • If you re-run the build, it will be quick because everything is cached.
  • If you change one line and re-run, that line and everything after will not be cached.
  • Keep things that change the most toward the bottom of the Dockerfile.

Extending Dockerfile

Custom Dockerfile for HTML page with nginx

FROM nginx:latest # Extends nginx so everything included in that image is included here
WORKDIR /usr/share/nginx/html
COPY index.html index.html

Build image from Dockerfile

docker image build -t nginx-website

Running it

docker container run -p 80:80 --rm nginx-website

Tag and push to Docker Hub

docker image tag nginx-website:latest btraversy/nginx-website:latest
docker image push bradtraversy/nginx-website

Volumes

Volume - Makes special location outside of container UFS. Used for databases.

Bind Mount - Link container path to host path.

Check volumes

docker volume ls

Cleanup unused volumes

docker volume prune

Pull down MySQL image to test

docker pull mysql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment