mirror of
https://github.com/ansible-collections/community.docker.git
synced 2026-07-29 11:55:04 +00:00
Add docker_image_build module. (#735)
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
azp/4
|
||||
destructive
|
||||
@@ -0,0 +1,10 @@
|
||||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
dependencies:
|
||||
- setup_docker_cli_buildx
|
||||
# The Python dependencies are needed for the other modules
|
||||
- setup_docker_python_deps
|
||||
- setup_remote_tmp_dir
|
||||
@@ -0,0 +1,13 @@
|
||||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
####################################################################
|
||||
# WARNING: These are designed specifically for Ansible tests #
|
||||
# and should not be used as examples of how to write Ansible roles #
|
||||
####################################################################
|
||||
|
||||
- when: ansible_facts.distribution ~ ansible_facts.distribution_major_version not in ['CentOS6', 'RedHat6']
|
||||
include_tasks:
|
||||
file: test.yml
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: "Loading tasks from {{ item }}"
|
||||
include_tasks: "{{ item }}"
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Create random name prefix
|
||||
set_fact:
|
||||
name_prefix: "{{ 'ansible-docker-test-%0x' % ((2**32) | random) }}"
|
||||
- name: Create image and container list
|
||||
set_fact:
|
||||
inames: []
|
||||
cnames: []
|
||||
|
||||
- debug:
|
||||
msg: "Using name prefix {{ name_prefix }}"
|
||||
|
||||
- name: Create files directory
|
||||
file:
|
||||
path: '{{ remote_tmp_dir }}/files'
|
||||
state: directory
|
||||
|
||||
- name: Template files
|
||||
template:
|
||||
src: '{{ item }}'
|
||||
dest: '{{ remote_tmp_dir }}/files/{{ item }}'
|
||||
loop:
|
||||
- ArgsDockerfile
|
||||
- Dockerfile
|
||||
- EtcHostsDockerfile
|
||||
- MyDockerfile
|
||||
- StagedDockerfile
|
||||
|
||||
- debug:
|
||||
msg: "Has buildx plugin: {{ docker_has_buildx }}"
|
||||
|
||||
- block:
|
||||
- include_tasks: run-test.yml
|
||||
with_fileglob:
|
||||
- "tests/*.yml"
|
||||
|
||||
always:
|
||||
- name: "Make sure all images are removed"
|
||||
docker_image:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
with_items: "{{ inames }}"
|
||||
- name: "Make sure all containers are removed"
|
||||
docker_container:
|
||||
name: "{{ item }}"
|
||||
state: absent
|
||||
force_kill: true
|
||||
with_items: "{{ cnames }}"
|
||||
|
||||
when: docker_api_version is version('1.25', '>=') and docker_cli_version is version('19.03', '>=') and docker_has_buildx
|
||||
|
||||
- fail: msg="Too old docker / docker-py version to run docker_image tests!"
|
||||
when: not(docker_api_version is version('1.25', '>=') and docker_cli_version is version('19.03', '>=')) and (ansible_distribution != 'CentOS' or ansible_distribution_major_version|int > 6) and docker_has_buildx
|
||||
@@ -0,0 +1,204 @@
|
||||
---
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
- name: Registering image name
|
||||
set_fact:
|
||||
iname: "{{ name_prefix ~ '-options' }}"
|
||||
|
||||
- name: Registering image name
|
||||
set_fact:
|
||||
inames: "{{ inames + [iname] }}"
|
||||
|
||||
####################################################################
|
||||
## args ############################################################
|
||||
####################################################################
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- name: buildargs
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
dockerfile: "ArgsDockerfile"
|
||||
args:
|
||||
IMAGE: "{{ docker_test_image_busybox }}"
|
||||
TEST1: val1
|
||||
TEST2: val2
|
||||
TEST3: "True"
|
||||
pull: false
|
||||
register: buildargs_1
|
||||
|
||||
- name: buildargs (idempotency)
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
dockerfile: "ArgsDockerfile"
|
||||
args:
|
||||
IMAGE: "{{ docker_test_image_busybox }}"
|
||||
TEST1: val1
|
||||
TEST2: val2
|
||||
TEST3: "True"
|
||||
pull: false
|
||||
register: buildargs_2
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- buildargs_1 is changed
|
||||
- buildargs_2 is not changed
|
||||
|
||||
####################################################################
|
||||
## dockerfile ######################################################
|
||||
####################################################################
|
||||
|
||||
- name: dockerfile
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
dockerfile: "MyDockerfile"
|
||||
pull: false
|
||||
register: dockerfile_1
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- dockerfile_1 is changed
|
||||
- "('FROM ' ~ docker_test_image_alpine) in dockerfile_1.stderr"
|
||||
- dockerfile_1['image']['Config']['WorkingDir'] == '/newdata'
|
||||
|
||||
####################################################################
|
||||
## platform ########################################################
|
||||
####################################################################
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- name: platform
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
platform: linux
|
||||
pull: false
|
||||
register: platform_1
|
||||
|
||||
- name: platform (idempotency)
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
platform: linux
|
||||
pull: false
|
||||
register: platform_2
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- platform_1 is changed
|
||||
- platform_2 is not changed
|
||||
|
||||
####################################################################
|
||||
## target ##########################################################
|
||||
####################################################################
|
||||
|
||||
- name: Build multi-stage image
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
dockerfile: "StagedDockerfile"
|
||||
target: first
|
||||
pull: false
|
||||
register: dockerfile_2
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- dockerfile_2 is changed
|
||||
- dockerfile_2.image.Config.WorkingDir == '/first'
|
||||
|
||||
####################################################################
|
||||
## etc_hosts #######################################################
|
||||
####################################################################
|
||||
|
||||
- name: Build image with custom etc_hosts
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
dockerfile: "EtcHostsDockerfile"
|
||||
pull: false
|
||||
etc_hosts:
|
||||
some-custom-host: "127.0.0.1"
|
||||
register: path_1
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- path_1 is changed
|
||||
|
||||
####################################################################
|
||||
## shm_size ########################################################
|
||||
####################################################################
|
||||
|
||||
- name: Build image with custom shm_size
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
dockerfile: "MyDockerfile"
|
||||
pull: false
|
||||
shm_size: 128MB
|
||||
register: path_1
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- path_1 is changed
|
||||
|
||||
####################################################################
|
||||
## labels ##########################################################
|
||||
####################################################################
|
||||
|
||||
- name: Build image with labels
|
||||
docker_image_build:
|
||||
name: "{{ iname }}"
|
||||
path: "{{ remote_tmp_dir }}/files"
|
||||
dockerfile: "MyDockerfile"
|
||||
pull: false
|
||||
labels:
|
||||
FOO: BAR
|
||||
this is a label: this is the label's value
|
||||
register: labels_1
|
||||
|
||||
- name: cleanup
|
||||
docker_image_remove:
|
||||
name: "{{ iname }}"
|
||||
|
||||
- name: Show image information
|
||||
debug:
|
||||
var: labels_1.image
|
||||
|
||||
- assert:
|
||||
that:
|
||||
- labels_1 is changed
|
||||
- labels_1.image.Config.Labels.FOO == 'BAR'
|
||||
- labels_1.image.Config.Labels["this is a label"] == "this is the label's value"
|
||||
@@ -0,0 +1,13 @@
|
||||
# Copyright (c) 2022, Felix Fontein
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
ARG IMAGE
|
||||
ARG TEST1
|
||||
ARG TEST2
|
||||
ARG TEST3
|
||||
|
||||
FROM ${IMAGE}
|
||||
ENV foo /bar
|
||||
WORKDIR ${foo}
|
||||
RUN echo "${TEST1} - ${TEST2} - ${TEST3}"
|
||||
@@ -0,0 +1,7 @@
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
FROM {{ docker_test_image_busybox }}
|
||||
ENV foo /bar
|
||||
WORKDIR ${foo}
|
||||
@@ -0,0 +1,7 @@
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
FROM {{ docker_test_image_busybox }}
|
||||
# This should fail building if docker cannot resolve some-custom-host
|
||||
RUN ping -c1 some-custom-host
|
||||
@@ -0,0 +1,9 @@
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
FROM {{ docker_test_image_alpine }}
|
||||
ENV INSTALL_PATH /newdata
|
||||
RUN mkdir -p $INSTALL_PATH
|
||||
|
||||
WORKDIR $INSTALL_PATH
|
||||
@@ -0,0 +1,11 @@
|
||||
# Copyright (c) Ansible Project
|
||||
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
FROM {{ docker_test_image_busybox }} AS first
|
||||
ENV dir /first
|
||||
WORKDIR ${dir}
|
||||
|
||||
FROM {{ docker_test_image_busybox }} AS second
|
||||
ENV dir /second
|
||||
WORKDIR ${dir}
|
||||
Reference in New Issue
Block a user