Basics of running a custom Docker container locally

February 26, 2022

Creating a custom image

Create a file named Dockerfile with your configuration. Although this post will not cover the ins and outs of this, here's an example of what I was using to modify the ghost image to support Amazon S3 storage.

FROM ghost:4.9.4

WORKDIR /var/lib/ghost

ENV AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
ENV AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
ENV AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION
ENV storage__active=s3
ENV storage__s3__bucket=$GHOST_STORAGE_ADAPTER_S3_PATH_BUCKET
ENV storage__s3__assetHost=$GHOST_STORAGE_ADAPTER_S3_ASSET_HOST
ENV storage__s3__serverSideEncryption=AES256
ENV storage__s3__acl=private
ENV url=$INSTANCE_DNS_NAME

RUN npm install -g ghost-storage-adapter-s3 && \
ln -s /usr/local/lib/node_modules/ghost-storage-adapter-s3 ./current/core/server/adapters/storage/s3

Breakdown:

  • FROM is the original image. You can use something like ubuntu if you want to start from scratch, though I encourage you to find an official image with more bells and whistles of what you're trying to accomplish if possible
  • WORKDIR is the directory that you'll be working out of in thee Docker instance.
  • ENV are environment variables that you can reference in the instance
  • RUN will run the commands in the terminal when you build your image

The ghost image I'm using here already has commands to start running so those are not set by me, but if you're creating your own own Dockerfile from scratch you'll need to start your server, database, etc. that you're building with the CMD or ENTRYPOINT actions.

Building your custom image

Assuming that you have a valid Dockerfile in your local directory, the following command will create a Docker container with a name that you can easily reference.

docker build -t <image-name> .

Example:

docker build -t website .

Running the container (isolated)

To run the container without any connection to your local file system, run:

docker run -d \
  -p <local-port>:<container-port> \
  --name <container-name> \
  <image-name>

Example:

docker run -d \
  -p 2368:2368 \
  --name website \
  website

Breakdown:

  • -d makes the container run in the background
  • -p maps the Docker port to your local port
  • --name sets a name for the instance that you can refer to later for restarting, stopping, removing, etc.
  • website is the name we assigned to the custom image we built earlier

Running the container (mounted to your local directory)

docker run -d \
  -v <local-directory>:<container-directory>
  -p <local-port>:<container-port> \
  --name <container-name> \
  <image-name>

Example:

docker run -d \
  -v $(pwd):/var/lib/ghost
  -p 2368:2368 \
  --name website \
  website

Viewing the logs

To view most recent logs, run:

docker logs <container-name>

To continue "tailing" the logs in your terminal, run:

docker logs <container-name> --follow

And when you're done following the container logs, hit <Cmd> + C to exit and go back to your terminal.

Stopping the container

docker stop <container-name>

Restarting the container

docker restart <container-name>

Deleting the container

If you've already stopped the container, you can run the rm command.

docker rm <container-name>

If the container has not been stopped, you can skip that step with the --force flag:

docker rm <container-name> --force

Other resources

Happy Dockering! SL