Example of taking a docker image and modifying it using a Dockerfile

Create the DockerFile

This dockerfile will specify an initial docker image and perform some tasks on it. This will result in the creation of a new image when we issue the docker build command.


In the following Dockerfile, we do the following:


  • specify the starting docker image
  • copy files into the image
  • run commands in the image
  • specify the internal ports that will be used by the image
  • define the entrypoint (what gets executed at image startup)



Dockerfile

Dockerfile
FROM jamesdbloom/mockserver
LABEL placodermi.component=mockserver
 
WORKDIR /opt/mockserver
 
# Artifacts
COPY entrypoint.sh /opt/mockserver/entrypoint.sh
COPY MockInitializer.java /opt/mockserver/MockInitializer.java
 
RUN chmod +x /opt/mockserver/entrypoint.sh
 
EXPOSE 1080
EXPOSE 1090
 
ENTRYPOINT ["/opt/mockserver/entrypoint.sh"]

Build the Image

Run the Dockerfile by issuing the following command

docker build -t <image_name> .   ( <-- don't miss that dot ) 

ie. > docker build -t mockserver_image . 

The above command will run the Dockerfile and create a new image with the <image_name> specified by the -t option.


If something goes wrong, you can remove the created image by issuing the following command:

docker rmi <image_name>

ie. > docker rim mockserver_image 

Run the image

Now that you have your image, you can run this image by issuing the following command:

docker run -it --rm <image_name>

ie. > docker run -it --rm mockserver_image


If you run into an error running the image, you can bypass your entrypoint and then access the filesystem. This may help diagnose the problem.

To log into your image issue the following command:

docker run -it --rm --entrypoint bash <image_name>

ie. > docker run -it --rm --entrypoint bash mockserver_image

Make a Container

Now that you have confirmed that the image works, you can make it a container by issuing the following command:

docker run -d -p <ex_port>:<int_port> -- name <container_name> <image>

ie. > docker run -d -p <ex_port>:<int_port> -- name mockserver mockserver_image 


That's the general basics of it all. 

  • No labels