Docker Commands Flashcards
Understand Docker Commands
FROM ruby:2.6
Start new container based on ruby:2.6 as base image
FROM ruby:2.6
what registry will this hit?
Docker Hub, most likely, as no other registry is specified
apt-get update -yqq
update apt-get
Update local package manifest with online registry. ` -y ` means say yes to any prompts. -qq
means do it quietly
RUN apt-get install -yqq --no-install-recommends nodejs
Install nodejs, do not install recommended (but not required) packages, say yes to all prompts, do it quietly
COPY . /usr/src/app/
Copy our rails app code at .
, always relative to where Dockerfile is, so it is baked into the image,
WORKDIR /usr/src/app
CD
into /usr/src/app
of image
RUN bundle install
run bundle install
within container’s current working directory
Run Rails Dockerfile
syntax to run test
container Rails server
docker run -p 3000:3000 test bin/rails s -b 0.0.0.0
what is this doing?
docker run -p 3000:3000 test bin/rails s -b 0.0.0.0
Runs docker container named test
, publish container port 3000 mapped to local port 3000, runs rails server on IP address 0.0.0.0
address 0.0.0.0 (localhost) port 3000 mapped to docker container port 30
what is the -b 0.0.0.0
doing here
bin/rails s -b 0.0.0.0
This is telling the rails server in the container to bind to all IPv4 addresses within the container. Our local host @ port 3000 only points to the IP address of the rails server in the container, but we have no idea what that IP address will be ahead of time, so we bind the Rails server to all IP addresses in the container
Docker command to list all images
docker images
Docker command to list running containers
docker ps
last command in Dockerfile to run Rails server as the default action on starting the container
Bonus: What type of command is this?
CMD ["bin/rails", "s", "-b", "0.0.0.0"]
`bin/rail
Exec form! bin/rails
is first command executed by container
The "-b", "0.0.0.0"
part is typically used in the context of a command to bind a service to all available IP addresses on the host machine.
In a Dockerfile, this is often seen in commands that start a server. For example, in a Ruby on Rails application, you might see:
```dockerfile
CMD [“rails”, “server”, “-b”, “0.0.0.0”]
~~~
Here’s what it does:
- -b
is a flag that stands for “bind”.
- 0.0.0.0
is a special IP address that tells the server to listen on all available network interfaces.
This is useful in a Docker container because it allows the service running inside the container to be accessible from outside the container, not just from within the container itself.
How does Dockerfile build caching work?
Docker will build a cache corresponding to each instruciton, or step, in the Dockerfile
What happens if a step or instruction changes in the Dockerfile
Docker must rebuild that step and all subsequent steps, cannot use cache
vim
is added later, why is this an issue?
RUN apt-get update -yqq RUN apt-get install\ -yqq --no-install-recommends\ nodejs ### becomes ... RUN apt-get update -yqq RUN apt-get install\ -yqq --no-install-recommends\ nodejs vim
RUN apt-get update -yqq
this line will be cached, so you never get updates for newly added packages in the next line that changes as packages are added
What does COPY Gemfile* /usr/src/app/
on its own line do for us?
COPY Gemfile* /usr/src/app/ WORKDIR /usr/src/app RUN bundle install
This allows Gemfile
and Gemfile.lock
to be cached in their own layer. This way, changes to the codebase, or other parts of the Dockerfile after this line, will not trigger a gemfile cache rebuild and re-bundle install
, which will happen if docker detects a change to the Gemfiles
what happens when you run docker-compose up
- D creates a speperate network just for the app
- Creates non-locally hosted volumes defined in the app
- Builds an image for any servies with a
build
directive - Creates a container for each services
- Launches a container per servies
Network, volumes, images, containers, launch
what is this line in docker-compose.yml doing?
volumes: - .:/usr/src/app
Mounts the CWD in our local machine to /usr/src/app
is COPY
always relative to where the Dockerfile is located? What about WORKDIR
changing this?
Yes. Even if you do WORKDIR
, this only changes the working dir in the image, RUN
commands are always run in WORKDIR
What does this do for us? Explain the utility. Why the --rm
flag?
docker-compose run --rm web rails db:migrate
This is how we run one-off instructions in our contianerized application. This asks a newly created web service container to run a rails command. It then deletes the spun off container with --rm
, otherwise that one-off container would remain.
Command to rebuild the Rails app (web service) image, with docker-compose
docker-compose build web
docker-compose command to rebuild web service
docker-compose build web
Docker Compose cmd to run command in already running web service container
docker-compose exec web <command>