You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

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

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. 

Creating a New Container in CloakedJS using Ansible

If you want to create a docker container in CloakedJS, have a look at the mockserver implementation in the following folders:

folder/file

Purpose

folder/file

Purpose

deployment/src/main/ansible/roles/builder.mockserver

Define the building of the image. Folder structure:

  • files - include your Dockerfile and any files you want to copy into the image

  • tasks - main.yml script to build the docker image
     name: "{{ docker_image }} -- Stop previous mock container"
    command: "docker stop {{ docker_image }}"
    ignore_errors: yes

    - name: "{{ docker_image }} -- Remove previous mock container"
    command: "docker rm --force {{ docker_image }}"
    ignore_errors: yes

    - name: "{{ docker_image }} -- Remove previous mock image"
    command: "docker rmi --force {{ docker_image }}-image"
    ignore_errors: yes

    - name: "{{ docker_image }} -- Build docker image"
    command: docker build -t {{ docker_image }}-image /vagrant/deployment/src/main/ansible/roles/builder.mockserver/files/


deployment/src/main/ansible/roles/deployer.mockserver

Define the building of the container.

Folder structure:

  • tasks - main.yml script to build the container

    - name: "endpoint-mock -- Stop old mock container"
    command: "docker stop endpoint-mock"
    ignore_errors: yes

    - name: "endpoint-mock -- Remove old mock container"
    command: "docker rm --force endpoint-mock"
    ignore_errors: yes

    - name: "{{ docker_image }} -- Run Mock docker image"
    command: docker run -d -p {{ docker_port }}:1080 --name {{ docker_image }} {{ docker_image }}-image
deployment/src/main/ansible/full_run_local.yml

Script to run the tasks mentioned above to build and deploy the docker image/container.

    • full_run_local.yml
      ....
      - { role: builder.mockserver, docker_image: mockserver }
      - { role: deployer.local.mockserver, docker_image: mockserver, docker_port: 12000}


  • No labels