The command above will create a container based on the docker image whose name is provided and returns an id for the container is has created. It will not start the container.
The command above will start up a container that has already been created by using the provided container id. It will run the default start-up command associated with the container to set it up (Two alternative versions of the command have been provided)
The first command above will create and start a container based on the docker image whose name is provided. It will run the default start-up command associated with the container to set it up (image will be pulled down from dockerhub automatically if it is not available locally)
The second command above will create and start a container based on the docker image whose name is provided. It will run the alternate startup command provided, however, instead of the default startup command associated with the container to set it up.
The first command above will create and start a container based on the docker image whose name is provided. It will run the default start-up command associated with the container to set it up; but it will run the container in the background and return control back to the shell in which it was run.
How to list all containers
$dockerps$dockerps--all$dockerps-a
The first command lists all containers currently running
The second command lists all containers that have ever been run
The third command is an alternate form of the second command
How to delete all containers
$dockercontainerprune--force
The command above will delete all containers
How to get logs from a running container
$dockerlogs<container-id>
The command above will print logs for the container whose id is provided
The first command requests the container whose id was passed to shut down. It waits 10 seconds and if the container has not shut down it kills it.
The second command kills the container whose id was provided without waiting 10 seconds right away.
How to run multiple container commands
$dockerexec-it<container-id><comand>
The command above attaches an interactive teletype to the running container whose id was provided and runs the provided command inside of the container.
A user can run the above multiple times, using a different command to run in the container each time.
The first command provides interactive shell access to a container that is running.
The second command creates and starts a container based on the provided image id. It runs the shellcommandinstead of running the startup command associated with the container.
How to build a custom image from a Dockerfile
# Define base image
FROM alpine
# Define dependencies
RUN apk add --update redis
# Define start-up command
CMD ["redis-server"]
$ docker run debugme/redis
$ docker run debugme/redis:latest
Dockerfile defines a docker file. The instructions inside it define how to build a custom redis image. The file must be called Dockerfile and can only contain Docker commands and comments.
Build image defines what is run from the folder in which the Dockerfile resides. It executes the instructions inside the Dockerfile to create an image with the name debugme/redis:latest and the dot is used to specify which directory the resources to include in the building of the image can be found in. The build command allows you to specify an alternate name for the Docker file with the --file flag.
Run image shows two equivalent ways to run this custom image. If latest is omitted, then it is assumed to be present by default.
How to build a tagged image from a container
// (1) startup a container from an alpine image
$ docker run -it alpine sh
// (2) install redis on the container
% apk add --update redis
// (3) in a new shell, create image from modified container
// with default start-up command
docker commit -c 'CMD ["redis-server"]' 06bbfbcbc3bf debugme/redis:home
// (4) Start up the image you just created
docker run debugme/redis:home
How to run a custom server in a docker container
# Dependencies least likely to change
FROM node:alpine
WORKDIR /home/mywebapp
EXPOSE 8080
CMD ["npm", "start"]
COPY ./package.json ./
RUN npm install
# Dependencies most likely to change
COPY ./ ./
constexpress=require('express')constserver=express()server.get('/', (request, response) =>response.send('hi there'))const { port=8080 } =process.envserver.listen(port, () =>console.log(`Server running on port ${port}`))
Dockerfile: instructions on how to create a custom image
Build image: the command at the terminal to build the image
Run image: terminal command to run image as a container
package.json: dependencies and scripts to build and run server
index.js: a JS code implementation for a simple server
How to delete all Docker images that have been downloaded
$ docker image prune --all --force
Docker-compose commands
$ docker-compose build // builds docker container(s)
$ docker-compose up // starts up docker container(s) in foreground
$ docker-compose up -d // starts up docker container(s) in background
$ docker-compose down // brings docker containers down
$ docker-compose ps // get the status of the running containers
How to run an application composed of multiple docker containers
$ docker-compose up --detach --build
$ docker-compose up -d --build
$ docker-compose up --detach
$ docker-compose up -d
$ docker-compose stop
// Note: Must be run in same directory as docker-compose.yml
$ docker-compose ps
// Note: Must be run in same directory as docker-compose.yml
// (1) Show logs of all services started up with docker-compose
$ docker-compose logs
// (2) Show logs of a specific service started up with docker-compose
$ docker-compose logs <container-id>
How to specify a restart policy for your docker container
To build the docker image, type the following into a terminal
$ docker build -t debugme/appserver --file Dockerfile.dev .
To run the docker container, type the following into a terminal
$ docker run -it -p 3000:3000 -v /app/node_modules -v $(pwd):/app debugme/appserver
To test that local changes are reflected in the container
(1) Connect to the server at http://localhost:3000 in your browser
(2) Modify the response message in App.js on your host machine
(3) Browser should automatically reload and you should see your changes
How to sync local frontend changes with Docker Compose
from node:alpine
workdir /usr/app
cmd ["npm", "start"]
copy package.json .
run npm install
copy . .
// The contents of your create react app's App.js file
// How to build the docker image specified in Dockerfile.dev
$ docker-compose build
// How to build container based on docker image just created
$ docker-compose up
(1) create the react application in your local directory i.e.
$ npx create-react-app .
(2) delete node_modules that create-react-app created in your local directory i.e.
$ rm -rf node_modules
(3) build an image based on your Dockerfile.dev
$ docker-compose build
(4) start up a container based on your built image
$ docker-compose up
(5) go to http://localhost:3010 to see the deployed react app
(6) make changes in your local directory and see them reflected in the browser
How to sync local backend changes with Docker Compose
FROM node:alpine
WORKDIR /usr/app
CMD ["npm", "start"]
COPY package.json .
RUN npm install
COPY . .
To build and run the docker, type the following into a terminal
$ docker-compose up --build
To test that local changes are reflected in the container
(1) Connect to the server at http://localhost:3000 in your browser
(2) Modify the response message in index.js
(3) Reload the browser and confirm you see the amended message
How to create a container using a multi-step approach
FROM node:alpine
WORKDIR /usr/app
CMD [ "npm", "start" ]
COPY package.json .
RUN npm install
COPY . .
FROM node:alpine as BUILD_STAGE
WORKDIR /usr/app
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=BUILD_STAGE /usr/app/build /usr/share/nginx/html
// How to create the react app on your local machine
$ create-react-app docker-react
$ cd docker-react
$ rm -rf node_modules
// How to do a development build and run
$ docker-compose up --build
// How to do a production build and run
$ docker build --tag debugme/app .
$ docker run --publish 8000:80 debugme/app
How to deploy code changes to GitHub and test pull requests on TravisCI
FROM node:alpine
WORKDIR /usr/docker-react
COPY package.json .
RUN npm install
COPY . .
CMD [ "npm", "start" ]
FROM node:alpine as BUILD_STAGE
WORKDIR /usr/docker-react
COPY package.json .
RUN npm install
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=BUILD_STAGE /usr/docker-react/build /usr/share/nginx/html
sudo: required
services:
- docker
before_install:
- docker build --file Dockerfile.dev --tag debugme/docker-react .
script:
- docker run debugme/docker-react npm run test -- --coverage
// How to create the react app on your local machine
$ create-react-app docker-react
$ cd docker-react
$ rm -rf node_modules
// How to do a development build and run
$ docker-compose up --build
// How to do a production build and run
$ docker build --tag debugme/docker-react .
$ docker run --publish 8000:80 debugme/docker-react
How to get container id via image name
// In the example below we find the container by looking for "35mm"
// which is part of the image name and then opening a shell on it
$ docker exec -it `docker ps | grep 35mm | awk '{ print $1}'` sh