Docker commands

I've been using Docker recently with a dev ops team on an authentication project whilst working with a UK bank. We are using multiple different micro services which work with a React front end. Each micro service is wrapped in its own docker container and we use one docker-compose file to run the entire stack from the individual images.

Create a Docker image


Before we create a docker image, we need to create a dockerfile which we can create the Docker image from. Here are the commands that can exist in our Dockerfile:


Create empty dockerfile

touch Dockerfile


The Dockerfile

FROM (the base image we are creating the docker image from)

MAINTAINER (optional - The name of the image maintainer)

RUN (used to execute the command for building the docker image)

ADD (you can use a url to add a file to the destination directory)

ENV (sets the environment variable e.g. 'FROM node:8')

WORKDIR (creates the ap directory e.g. '/usr/src/app')

EXPOSE (the port to expose e.g. 8080)

RUN (used to run commands before the build of the container e.g. 'npm install')
CMD (used to execute commands when creating the docker container e.g. [ 'npm', 'start' ])


Example dockerfile

FROM node:8


# app directory
WORKDIR /usr/src/app

# Install app dependencies
RUN npm install

# If production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 8080

CMD [ 'npm', 'start' ]


Build docker image from dockerfile

docker build -t NAME-OF-DOCKER-IMAGE .

List all docker images

docker images

Run docker image

docker run NAME-OF-DOCKER-IMAGE -p 80:80

Show all docker containers

docker ps