TR-20240114 // PUBLIC RELEASE

Docker Refresher ๐Ÿน

Released by
Prayag Bhakar
Series
Refreshers ๐Ÿน
Release date
Revised
improve prose and fix grammer
Length
279 words ยท 2 min ยท grade 7
Subjects
#docker#refresher

Pop open a refreshing beverage and freshen up on Docker and Docker Compose. Every flag here has a longer story in the Docker docs.

1โ€‚Docker command line

# show all containers
$ docker ps
# list all containers, including stopped ones
$ docker container ls --all

# stop a container
$ docker stop example
# start a stopped container
$ docker start example

# restart a running container
$ docker restart <container-id>

# remove a stopped container
$ docker rm example
# only stopped containers can be removed
$ docker rm <stopped-container-id>

2โ€‚Dockerfile

A pseudo example of a multi-stage Dockerfile.

FROM alpine:latest as builder
# set the working directory
WORKDIR /build
# run any shell command during build
RUN apk add --no-cache npm
# copy files and folders
COPY ./source /build
# commands to run when the container starts
CMD ["npm", "run", "build"]

FROM alpine:latest
WORKDIR /serv
RUN apk add --no-cache npm
# copy files from different stages
COPY --from=builder /build /serv 
# what ports the container should listen to at runtime
EXPOSE 420
# this is the container's main executable
ENTRYPOINT ["npm", "start"]

To build and run this Dockerfile.

$ docker build . --tag example:1.0

# this publishes port 69 on every host interface, regardless of firewall config
# use --publish 127.0.0.1:96:420 to bind to the host
$ docker run --publish 69:420 example

# open an interactive shell (like connecting to a server)
# or replace sh with any command
$ docker exec --interactive --tty <container-id> sh
# exit the container
$ exit

3โ€‚docker-compose

As you collect Dockerfiles, a Compose file makes orchestrating them easier.

version: '3.9'

services:
  notebook:
    image: jupyter/minimal-notebook
    container_name: jupyter
    hostname: jupyter
    restart: always
    networks: 
      - tunnel
    ports:
      - "90:8888"
    volumes:
      - jupyter_data:/home/jupyter
    runtime: nvidia
    user: root
    command: "start-notebook.sh"
    environment:
      NB_USER: jupyter
      NB_UID: 1000
      NB_GID: 1000
      CHOWN_HOME: 'yes'
      CHOWN_HOME_OPTS: -R
      NVIDIA_VISIBLE_DEVICES: 'all'

volumes: 
  jupyter_data:

networks:
  tunnel:
    driver: bridge

TR-20240114 // PUBLIC RELEASE

distribution per CC BY 4.0

Backlinks