Search:   Dictionary All Posts
Store Your Knowledge at the Brain Bank!

Docker - Setting Up Dockerfile, Docker Commands

By Jamie in Lessons / Programming - General  2.20.19  
Summary - Basic Dockerfile setup and Docker commands. Docker Compose. Use proper docker commands to create containers from Image files or use Docker Compose to create containers from docker-compose.yml file.
Initial Setup

Image  - Dockerfile
touch Dockerfile in the root directory of your project.

Dockerfile
FROM node:11.7.0

CMD ["/bin/bash"]
// this line, when run, tells Docker to go to the shell of the user's computer when a command is run. in other words, it allows Docker commands to be run in the shell you are using.

Docker Commands

docker build -t name-of-project-container .
// this creates the container of the given name
// include the dot . after to include all
// this will build the container as it reads the Dockerfile image
// it goes through the Dockerfile line by line - in our example, the first line, it creates the version of node - then it runs the CMD (command)
// after you create the container the first time, the following times you create it, assuming no changes were made, it will run from the cache and thus be re-created much quicker

docker run -it name-of-project-container
// every time 'docker run' is run, Docker creates a new Image hash because every time it runs it will be unique Image
// this command brings you into the container
// once in the container you can use node commands, like ---- node -v ---- to see you are in the correct version of node that was referenced in the Image file. It is important to always test these things to be sure the container was created correctly.

exit
// the exit command gets you out of the container

If you make any changes to the Image you want to run the build command again

Moving Along...

docker run -it -d name-of-project-container
// this command runs the container, but keeps it running the background so you don't have to restart it all the time

docker ps
this will show you all the containers running as well as a bunch of details about each container that was running

docker exec -it name-of-container bash
// once multiple containers are running you can get into one specific container in your shell using this command

exit
// this gets you out of the container you are in

docker stop name-of-container
// if multiple containers are running, this command will stop that container from running

Expanding Our Dockerfile Image

FROM node:version#

WORKDIR /user/src/project-directory-name
# comments in this file use pound
# when the Image is run and the container created, it will set WORKDIR to the main directory of the container

COPY ./ ./
# this will copy all files from the root directory of the local machine directory that you are currently in, into the root directory of the container

RUN npm install
# this will run the npm install command in the container, which will read the package.json file from the above copied directory and install all dependencies as normally would happen when you first run npm install on a cloned project
# there can be multiple run commands

CMD ["/bin/bash"]
# there can be only one CMD

// now when you run...
docker build -t name-of-project-container .
the Docker build engine will create the name-of-project-container, then it will read the Dockerfile Image one line at a time, first it installs node, then sets the working directory, then copies all files form the current directory into the current directory of the container, then runs npm install which reads the package.json file and installs all dependencies, then runs the CMD to setup bash

docker run -it name-of-project-container
this will run the container and bring you into the directory you specified in WORKDIR within the Docker Container.

Now, if you run 'npm start' after preceding directions you will not actually be able to connect because you need to bind the Port of the localhost to the Docker container.

docker run -it -p 3000:3000 name-of-project-container
this will run the container and using the command -p will connect the localhost port on the left to the container port host on the right