Skip to content

Encoding issue when calling importImage with changes #785

@Clovel

Description

@Clovel

Issue

It seems that there is an encoding issue when calling importImage with the changes parameter.

When passing changes as a string array to the function, I get the following error :

(HTTP code 400) unexpected - ["EXPOSE is not a valid change command

If I call encodingURIComponent on all the elements of the changes array, I get the following error :

(HTTP code 400) unexpected - ["EXPOSE%2022%2Ftcp","EXPOSE%2080%2Ftcp", ....... entrypoint%22%5D"] is not a valid change command

It is interesting to see that the docs specify that elements of the changes array should be URI encoded : https://docs.docker.com/reference/api/engine/version/v1.43/#tag/Image/operation/ImageCommit

I use to have a working "import" function before using this one, but it would missinterpret the brackets of ENTRYPOINT & CMD changes. Here is it's implementation :

export type ImportDockerImageOptions = {
  repo: string;
  tag: string;
  platform?: string;
} & {
  changes?: string[];
} &
(
  {
    fromSrc: string;
    message?: string;
  } |
  {
    fromImage: string;
  }
);

export const importDockerImage = (
  docker: Dockerode,
  file: string | NodeJS.ReadableStream,
  options: ImportDockerImageOptions,
): Promise<NodeJS.ReadableStream> => {
  const queryString = qs
    .encode(options)
    .replace(/%5B/g, '[')
    .replace(/%5D/g, ']');

  // console.log(`[DEBUG] importDockerImage query string : ${queryString}`);

  const optsf: DockerModem.DialOptions = {
    path: `/images/create?${queryString}`,
    method: 'POST',
    options: {},
    file: file,
    abortSignal: undefined,
    isStream: true,
    statusCodes: {
      200: true,
      404: `repository ${options.repo} does not exist or no read access`,
      500: 'server error',
    },
  };

  return new Promise((resolve, reject) => {
    docker.modem.dial(
      optsf,
      (err, data) => {
        if(err) {
          return reject(err);
        }
        resolve(data as NodeJS.ReadableStream);
      });
  });
};

In the beginning of the function you can see that I process brackets in a special way to make it work. Otherwise I get errors from the Docker API. My ENTRYPOINT bug lied there.

I was hoping that using the "official" importImage function would fix my issue AND make my code more standard.

TODO

  • Fix the encoding issue
  • Document the behaviour of the function with regards to the changes options
  • Declare a type for the options parameter of the function
  • Update tests

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions